Command Line Argument Processing

Discussion in 'Ruby' started by robertlaferla@comcast.net, Jan 9, 2007.

  1. Guest

    What is everyone using (other than manually) to process command line arguments? I have looked at optparse (OptionParser) but it is not well documented. Is there something better? Or is there better documentation for optparse out there?
     
    , Jan 9, 2007
    #1
    1. Advertising

  2. On 1/9/07, <> wrote:
    > What is everyone using (other than manually) to process command line arguments? I have looked at optparse (OptionParser) but it is not well documented. Is there something better? Or is there better documentation for optparse out there?


    Do you have Programming Ruby?

    I found the documentation on p711-712 to be sufficient.

    An example can be found at:
    http://stdlib.rubyonrails.org/libdoc/optparse/rdoc/index.html
     
    Gregory Brown, Jan 9, 2007
    #2
    1. Advertising

  3. On 1/9/07, <> wrote:
    > What is everyone using (other than manually) to process command line argu=

    ments? I have looked at optparse (OptionParser) but it is not well documen=
    ted. Is there something better? Or is there better documentation for optp=
    arse out there?
    >


    I use optparse and it fits my needs pretty well. I've started with the
    example found here
    http://www.ruby-doc.org/core/
    and I could do every thing I want. One example is often better than a
    thousand of documentation lines.

    I hope this help you.

    Cheers,

    --=20
    Nicolas Despr=E8s
     
    Nicolas Desprès, Jan 9, 2007
    #3
  4. wrote:
    > What is everyone using (other than manually) to process command line
    > arguments? I have looked at optparse (OptionParser) but it is not
    > well documented. Is there something better? Or is there better
    > documentation for optparse out there?


    OptionParser is very good, and generates reasonably nice on-line help.

    You might want to look at http://rubyforge.org/projects/optionparser/ as
    well (I haven't used it, but some people seem to like it).

    If you want to use something very simple that you can paste into a
    script and hack up as you see fit, there is argos:

    http://redshift.sourceforge.net/argos.rb

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jan 9, 2007
    #4
  5. Phrogz Guest

    wrote:
    > What is everyone using (other than manually) to process command line arguments?
    > I have looked at optparse (OptionParser) but it is not well documented. Is there
    > something better? Or is there better documentation for optparse out there?


    Like you, I was confused by the documentation for optparse and
    getoptlong. So, for a while, I was using the Usage gem:
    http://rubyforge.org/projects/usage/

    You have to install the gem and then look at its documentation to
    figure out how to use it, but I found it reasonably easy. Eventually,
    though, I got tired of some if its limitations and started writing my
    own template code (sample follows). It's certainly a manual process,
    but I liked feeling the wind in my hair.


    USAGE = <<ENDUSAGE
    Usage:
    findfile [-d max_depth] [-a] [-c] [-i] name_regexp [content_regexp]
    -d,--depth the maximum depth to recurse to (defaults to no
    limit)
    -a,--showall with content_regexp, show every match per file
    (defaults to only show the first-match per file)
    -c,--usecase with content_regexp, use case-sensitive matching
    (defaults to case-insensitive)
    -i,--includedirs also find directories matching name_regexp
    (defaults to files only; incompatible with
    content_regexp)
    -h,--help show some help examples
    ENDUSAGE

    EXAMPLES = <<ENDEXAMPLES

    Examples:
    findfile foo
    # Print the path to all files with 'foo' in the name

    findfile -i foo
    # Print the path to all files and directories with 'foo' in the name

    findfile js$
    # Print the path to all files whose name ends in "js"

    findfile js$ vector
    # Print the path to all files ending in "js" with "Vector" or
    "vector"
    # (or "vEcTOr", "VECTOR", etc.) in the contents, and print some of
    the
    # first line that has that content.

    findfile js$ -c Vector
    # Like above, but must match exactly "Vector" (not 'vector' or
    'VECTOR').

    findfile . vector -a
    # Print the path to every file with "Vector" (any case) in it
    somewhere
    # printing every line in those files (with line numbers) with that
    content.

    findfile -d 0 .
    # Print the path to every file that is in the current directory.

    findfile -d 1 .
    # Print the path to every file that is in the current directory or
    any
    # of its child directories (but no subdirectories of the children).
    ENDEXAMPLES

    ARGS = {}
    UNFLAGGED_ARGS = [ :name_regexp, :content_regexp ]
    next_arg = UNFLAGGED_ARGS.first
    ARGV.each{ |arg|
    case arg
    when '-d','--depth'
    next_arg = :max_depth
    when '-a','--showall'
    ARGS[:showall] = true
    when '-c','--usecase'
    ARGS[:usecase] = true
    when '-i','--includedirs'
    ARGS[:includedirs] = true
    when '-h','--help'
    ARGS[:help] = true
    else
    if next_arg
    if next_arg==:max_depth
    arg = arg.to_i + 1
    end
    ARGS[next_arg] = arg
    UNFLAGGED_ARGS.delete( next_arg )
    end
    next_arg = UNFLAGGED_ARGS.first
    end
    }

    if ARGS[:help] or !ARGS[:name_regexp]
    puts USAGE
    puts EXAMPLES if ARGS[:help]
    exit
    end
     
    Phrogz, Jan 10, 2007
    #5
  6. wrote:
    > What is everyone using (other than manually) to process command line arguments? I have looked at optparse (OptionParser) but it is not well documented. Is there something better? Or is there better documentation for optparse out there?


    Fed up with some quirks of getoptlong, and confused by optparse, I
    wrote (and use) my own library called "getopt". It includes two
    classes - Getopt::Std and Getopt::Long. The former is nearly identical
    to the Perl equivalent, while the latter is more or less a refactored
    version of getoptlong.

    http://raa.ruby-lang.org/project/getopt/

    Regards,

    Dan
     
    Daniel Berger, Jan 10, 2007
    #6
  7. gga Guest

    ha escrito:

    > What is everyone using (other than manually) to process command line arguments? I have looked at
    > optparse (OptionParser) but it is not well documented. Is there something better? Or is there better
    > documentation for optparse out there?


    Getopt::Declare, for everything, since my good old Perl days, as
    there's still nothing better.

    Example:

    #!/usr/bin/env ruby

    require 'Getopt/Declare'

    args = Getopt::Declare.new(<<'EOF')
    -q[uiet] don't print status messages to stdout
    but multi-line. This allows something.
    -f[ile] <file:eek:f> write report to file
    EOF

    p args

    # The example above makes it support -q, -quiet and -f, -file as output
    file.
    # -h[elp] and -v[ersion] come for free, too

    How to get it:

    http://rubyforge.org/projects/getoptdeclare/

    or:

    > gem install getopt-declare


    Docs are not online (my fault, as they were long and rubyforge was
    still in its infancy at the time).
    Usage is almost identical to its Perl's cousin, so you can look at the
    docs there.
    The gem DOES ship with an rdoc file called Declare.rdoc, which
    unfortunately rubygem's system still cannot 'ri' automatically, so you
    need to 'ri' it manually.
    Getopt-Declare uses a very complex parser behind the scenes, where you
    just type in the help line description (all that getopt-declare needs).
    Don't be deceived by its simplicity (it has more features than any
    other command-line parser). It comes with a bunch of much more complex
    demos in the samples directory of the gem distro.

    The one gotcha, you just need a tab between your argument and your
    description (at least until my next release, where I *finally* remove
    that limitation and allow spaces, too)

    PS. I cannot take credit for Getopt/Declare, as it was something
    Damian Conway came up many moons ago. I just ported it to ruby and
    fixed some bugs here and there.
     
    gga, Jan 11, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hal styli
    Replies:
    6
    Views:
    409
  2. Gregor H.

    Processing command line arguments

    Gregor H., Apr 21, 2007, in forum: C Programming
    Replies:
    5
    Views:
    523
  3. News
    Replies:
    8
    Views:
    339
    James Kanze
    Sep 17, 2007
  4. Jim Carter

    command line text processing..

    Jim Carter, Nov 10, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    103
    Tad McClellan
    Nov 10, 2003
  5. miracle_ks
    Replies:
    2
    Views:
    117
    Joe Smith
    Sep 27, 2004
Loading...

Share This Page