Skip to content

Instantly share code, notes, and snippets.

@rsanheim
Forked from rtomayko/optparse-template.rb
Created December 28, 2015 18:07

Revisions

  1. @rtomayko rtomayko revised this gist Oct 18, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions optparse-template.rb
    Original file line number Diff line number Diff line change
    @@ -14,10 +14,10 @@
    # parse arguments
    file = __FILE__
    ARGV.options do |opts|
    opts.on("-f", "--flag") { |flag| }
    opts.on("-o", "--opt=val", String) { |option| }
    opts.on("-i", "--int=val", Integer) { |integer| }
    opts.on("--list=[x,y,z]", Array) { |list| }
    opts.on("-f", "--flag") { flag = true }
    opts.on("-o", "--opt=val", String) { |val| option = val }
    opts.on("-i", "--int=val", Integer) { |val| integer = val }
    opts.on("--list=[x,y,z]", Array) { |val| list = val }
    opts.on_tail("-h", "--help") { exec "grep ^#/<'#{file}'|cut -c4-" }
    opts.parse!
    end
  2. @rtomayko rtomayko revised this gist Sep 3, 2011. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions optparse-template.rb
    Original file line number Diff line number Diff line change
    @@ -12,15 +12,13 @@
    list = ["x", "y", "z"]

    # parse arguments
    file = __FILE__
    ARGV.options do |opts|
    opts.on("-f", "--flag") { |flag| }
    opts.on("-o", "--opt=val", String) { |option| }
    opts.on("-i", "--int=val", Integer) { |integer| }
    opts.on("--list=[x,y,z]", Array) { |list| }
    opts.on_tail("-h", "--help") do
    puts File.read(__FILE__).grep(/^#\//).map {|line| line[3...-1]}.join("\n")
    exit
    end
    opts.on_tail("-h", "--help") { exec "grep ^#/<'#{file}'|cut -c4-" }
    opts.parse!
    end

  3. @rtomayko rtomayko revised this gist Sep 3, 2011. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions optparse-template.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,6 @@
    option = "default value"
    integer = 23
    list = ["x", "y", "z"]
    dest = File.expand_path(File.dirname($0))

    # parse arguments
    ARGV.options do |opts|
    @@ -26,8 +25,8 @@
    end

    # do your thing
    warn "arguments: #{ARGV}"
    warn "flag: #{flag.inspect}"
    warn "option: #{option.inspect}"
    warn "integer: #{integer.inspect}"
    warn "list: #{list.join(',')}"
    warn "ARGV: #{ARGV.inspect}"
    warn "flag: #{flag.inspect}"
    warn "option: #{option.inspect}"
    warn "integer: #{integer.inspect}"
    warn "list: #{list.join(',')}"
  4. @rtomayko rtomayko revised this gist Sep 3, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions optparse-template.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    #/ Usage: <progname> [options]...
    #/ How does this script make my life easier?
    # ** Tip: use #/ lines to define the --help usage message.
    $stderr.sync = true
    require 'optparse'

    # default options
  5. @rtomayko rtomayko revised this gist Sep 3, 2011. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion optparse-template.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    #!/usr/bin/env ruby
    #/ Usage: <progname> [options]...
    #/ How does this script make my life easier?
    #/
    # ** Tip: use #/ lines to define the --help usage message.
    require 'optparse'

  6. @rtomayko rtomayko revised this gist Sep 3, 2011. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions optparse-template.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    #!/usr/bin/env ruby
    #/ Usage: <progname> [options]...
    #/ How does this program make my life easier?
    #/ How does this script make my life easier?
    #/
    # ** Tip: use #/ lines to define the --help usage message.
    require 'optparse'

    # default options
    @@ -16,7 +18,10 @@
    opts.on("-o", "--opt=val", String) { |option| }
    opts.on("-i", "--int=val", Integer) { |integer| }
    opts.on("--list=[x,y,z]", Array) { |list| }
    opts.on_tail("-h", "--help") { exit }
    opts.on_tail("-h", "--help") do
    puts File.read(__FILE__).grep(/^#\//).map {|line| line[3...-1]}.join("\n")
    exit
    end
    opts.parse!
    end

    @@ -25,4 +30,4 @@
    warn "flag: #{flag.inspect}"
    warn "option: #{option.inspect}"
    warn "integer: #{integer.inspect}"
    warn "list: #{list.join(',')}"
    warn "list: #{list.join(',')}"
  7. @rtomayko rtomayko created this gist Sep 3, 2011.
    28 changes: 28 additions & 0 deletions optparse-template.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/usr/bin/env ruby
    #/ Usage: <progname> [options]...
    #/ How does this program make my life easier?
    require 'optparse'

    # default options
    flag = false
    option = "default value"
    integer = 23
    list = ["x", "y", "z"]
    dest = File.expand_path(File.dirname($0))

    # parse arguments
    ARGV.options do |opts|
    opts.on("-f", "--flag") { |flag| }
    opts.on("-o", "--opt=val", String) { |option| }
    opts.on("-i", "--int=val", Integer) { |integer| }
    opts.on("--list=[x,y,z]", Array) { |list| }
    opts.on_tail("-h", "--help") { exit }
    opts.parse!
    end

    # do your thing
    warn "arguments: #{ARGV}"
    warn "flag: #{flag.inspect}"
    warn "option: #{option.inspect}"
    warn "integer: #{integer.inspect}"
    warn "list: #{list.join(',')}"