Guest User

Untitled

a guest
Jan 17th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. ...
  2. $quiet = ARGV.delete('-d')
  3. $interactive = ARGV.delete('-i')
  4. ...
  5. # Deal with ARGV as usual here, maybe using ARGF or whatever.
  6.  
  7. opts = Trollop::options do
  8. opt :quiet, "Use minimal output", :short => 'q'
  9. opt :interactive, "Be interactive"
  10. opt :filename, "File to process", :type => String
  11. end
  12.  
  13. % cat temp.rb
  14. require 'optparse'
  15. OptionParser.new do |o|
  16. o.on('-d') { |b| $quiet = b }
  17. o.on('-i') { |b| $interactive = b }
  18. o.on('-f FILENAME') { |filename| $filename = filename }
  19. o.on('-h') { puts o; exit }
  20. o.parse!
  21. end
  22. p :quiet => $quiet, :interactive => $interactive, :filename => $filename
  23. % ruby temp.rb
  24. {:interactive=>nil, :filename=>nil, :quiet=>nil}
  25. % ruby temp.rb -h
  26. Usage: temp [options]
  27. -d
  28. -i
  29. -f FILENAME
  30. -h
  31. % ruby temp.rb -d
  32. {:interactive=>nil, :filename=>nil, :quiet=>true}
  33. % ruby temp.rb -i
  34. {:interactive=>true, :filename=>nil, :quiet=>nil}
  35. % ruby temp.rb -di
  36. {:interactive=>true, :filename=>nil, :quiet=>true}
  37. % ruby temp.rb -dif apelad
  38. {:interactive=>true, :filename=>"apelad", :quiet=>true}
  39. % ruby temp.rb -f apelad -i
  40. {:interactive=>true, :filename=>"apelad", :quiet=>nil}
  41.  
  42. #!/usr/bin/env ruby
  43.  
  44. def usage(s)
  45. $stderr.puts(s)
  46. $stderr.puts("Usage: #{File.basename($0)}: [-l <logfile] [-q] file ...")
  47. exit(2)
  48. end
  49.  
  50. $quiet = false
  51. $logfile = nil
  52.  
  53. loop { case ARGV[0]
  54. when '-q' then ARGV.shift; $quiet = true
  55. when '-l' then ARGV.shift; $logfile = ARGV.shift
  56. when /^-/ then usage("Unknown option: #{ARGV[0].inspect}")
  57. else break
  58. end; }
  59.  
  60. # Program carries on here.
  61. puts("quiet: #{$quiet} logfile: #{$logfile.inspect} args: #{ARGV.inspect}")
  62.  
  63. #!/usr/bin/env ruby -s
  64. puts "#$0: Quiet=#$q Interactive=#$i, ARGV=#{ARGV.inspect}"
  65.  
  66. $ ./test
  67. ./test: Quiet= Interactive=, ARGV=[]
  68. $ ./test -q foo
  69. ./test: Quiet=true Interactive=, ARGV=["foo"]
  70. $ ./test -q -i foo bar baz
  71. ./test: Quiet=true Interactive=true, ARGV=["foo", "bar", "baz"]
  72. $ ./test -q=very foo
  73. ./test: Quiet=very Interactive=, ARGV=["foo"]
  74.  
  75. options = Parser.new do |p|
  76. p.version = "fancy script version 1.0"
  77. p.option :verbose, "turn on verbose mode"
  78. p.option :number_of_chairs, "defines how many chairs are in the classroom", :default => 1
  79. p.option :room_number, "select room number", :default => 2, :value_in_set => [1,2,3,4]
  80. end.process!
  81.  
  82. Usage: micro-optparse-example [options]
  83. -v, --[no-]verbose turn on verbose mode
  84. -n, --number-of-chairs 1 defines how many chairs are in the classroom
  85. -r, --room-number 2 select room number
  86. -h, --help Show this message
  87. -V, --version Print version
  88.  
  89. require 'optiflag'
  90.  
  91. module Whatever extend OptiFlagSet
  92. flag "f"
  93. and_process!
  94. end
  95.  
  96. ARGV.flags.f # => .. whatever ..
  97.  
  98. def main
  99. ARGV.each { |a| eval a }
  100. end
  101.  
  102. main
  103.  
  104. if( ARGV.include( '-f' ) )
  105. file = ARGV[ARGV.indexof( '-f' ) + 1 )]
  106. ARGV.delete('-f')
  107. ARGV.delete(file)
  108. end
  109.  
  110. class MyApp < Thor # [1]
  111. map "-L" => :list # [2]
  112.  
  113. desc "install APP_NAME", "install one of the available apps" # [3]
  114. method_options :force => :boolean, :alias => :optional # [4]
  115. def install(name)
  116. user_alias = options[:alias]
  117. if options.force?
  118. # do something
  119. end
  120. # ... other code ...
  121. end
  122.  
  123. desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
  124. def list(search = "")
  125. # list everything
  126. end
  127. end
  128.  
  129. app install myname --force
  130.  
  131. MyApp.new.install("myname")
  132. # with {'force' => true} as options hash
  133.  
  134. case ARGV.join
  135. when /-h/
  136. puts "help message"
  137. exit
  138. when /-opt1/
  139. puts "running opt1"
  140. end
  141.  
  142. # example
  143. # script.rb -u username -p mypass
  144.  
  145. # check if there are even set of params given
  146. if ARGV.count.odd?
  147. puts 'invalid number of arguments'
  148. exit 1
  149. end
  150.  
  151. # holds key/value pair of cl params {key1 => value1, key2 => valye2, ...}
  152. opts = {}
  153.  
  154. (ARGV.count/2).times do |i|
  155. k,v = ARGV.shift(2)
  156. opts[k] = v # create k/v pair
  157. end
  158.  
  159. # set defaults if no params are given
  160. opts['-u'] ||= 'root'
  161.  
  162. # example use of opts
  163. puts "username:#{opts['-u']} password:#{opts['-p']}"
  164.  
  165. opts = {}
  166.  
  167. (ARGV.count/2).times do |i|
  168. k,v = ARGV.shift(2)
  169. opts[k] = v # create k/v pair
  170. end
  171.  
  172. other_args = Clap.run ARGV,
  173. "-s" => lambda { |s| switch = s },
  174. "-o" => lambda { other = true }
  175.  
  176. (options = []) << Acclaim::Option.new(:verbose, '-v', '--verbose')
  177. values = Acclaim::Option::Parser.new(ARGV, options).parse!
  178. puts 'Verbose.' if values.verbose?
  179.  
  180. cmd.rb
  181. cmd.rb action
  182. cmd.rb action -a -b ...
  183. cmd.rb action -ab ...
  184.  
  185. ACTION = ARGV.shift
  186. OPTIONS = ARGV.join.tr('-', '')
  187.  
  188. if ACTION == '***'
  189. ...
  190. if OPTIONS.include? '*'
  191. ...
  192. end
  193. ...
  194. end
  195.  
  196. def usage
  197. "usage: #{File.basename($0)}: [-l=<logfile>] [-q] file ..."
  198. end
  199.  
  200. $quiet = false
  201. $logfile = nil
  202.  
  203. ARGV.delete_if do |cur|
  204. next false if cur[0] != '-'
  205. case cur
  206. when '-q'
  207. $quiet = true
  208. when /^-l=(.+)$/
  209. $logfile = $1
  210. else
  211. $stderr.puts "Unknown option: #{cur}"
  212. $stderr.puts usage
  213. exit 1
  214. end
  215. end
  216.  
  217. puts "quiet: #{$quiet} logfile: #{$logfile.inspect} args: #{ARGV.inspect}"
  218.  
  219. opts = ParseOpt.new
  220. opts.usage = "git foo"
  221.  
  222. opts.on("b", "bool", help: "Boolean") do |v|
  223. $bool = v
  224. end
  225.  
  226. opts.on("s", "string", help: "String") do |v|
  227. $str = v
  228. end
  229.  
  230. opts.on("n", "number", help: "Number") do |v|
  231. $num = v.to_i
  232. end
  233.  
  234. opts.parse
  235.  
  236. ## Options:
  237. ## -i, --interactive Interactive mode
  238. ## -q, --quiet Silent mode
  239.  
  240. require 'easyoptions'
  241. unless EasyOptions.options[:quiet]
  242. puts 'Interactive mode enabled' if EasyOptions.options[:interactive]
  243. EasyOptions.arguments.each { |item| puts "Argument: #{item}" }
  244. end
Add Comment
Please, Sign In to add comment