Argument parsing! 
Author Message
 Argument parsing!

We've written a new command-line argument parsing library for use with
Gwydion Dylan. It's portable, and is intended to work with Harlequin Dylan
programs as well. Macro support is not required.

If anybody needs such a library, we'll make a package separate from the
main Gwydion Dylan distribution. The library may, of course, be used in
proprietary software without any problems.

It supports short options, long options, options with and without
parameters, and even "-DFOO=BAR"-style options. It knows about the "--"
convention. If you need new kinds of options (say, a "-v" flag which makes
output more verbose each time it appears), you can extended it easily. It
does have a bit of a bias, though: it supports the POSIX and GNU styles of
arguments, and ignores other conventions.

I've attached an example main() routine for the curious.

Cheers,
Eric

--- sample code ---

define method main(progname :: <string>, #rest argv)
  let *parser* = make(<argument-list-parser>);
  add-option-parser-by-type(*parser*,
                            <simple-option-parser>,
                            long-options: #("verbose"),
                            short-options: #("v"),
                            negative-long-options: #("quiet"),
                            negative-short-options: #("q"),
                            default: #t);
  add-option-parser-by-type(*parser*,
                            <simple-option-parser>,
                            long-options: #("foo"),
                            short-options: #("f"),
                            negative-long-options: #("no-foo"),
                            negative-short-options: #("B"),
                            default: #f);
  add-option-parser-by-type(*parser*,
                            <parameter-option-parser>,
                            long-options: #("quux"),
                            short-options: #("Q"));
  add-option-parser-by-type(*parser*,
                            <repeated-parameter-option-parser>,
                            long-options: #("warning"),
                            short-options: #("W"));
  add-option-parser-by-type(*parser*,
                            <keyed-option-parser>,
                            long-options: #("define"),
                            short-options: #("D"));

  unless(parse-arguments(*parser*, argv))
    format-out("usage: getopt-test [-qvfB] [-Q arg] [-W arg]* "
                 "[-Dkey[=value]]*\n");
    exit(exit-code: 1);
  end unless;

  let verbose? = option-value-by-long-name(*parser*, "verbose");
  let foo? = option-value-by-long-name(*parser*, "foo");
  let quux = option-value-by-long-name(*parser*, "quux");
  let warnings = option-value-by-long-name(*parser*, "warning");
  let defines = option-value-by-long-name(*parser*, "define");

  format-out("Program name:    %s\n", progname);
  format-out("Verbose?:        %=\n", verbose?);
  format-out("Foo?:            %=\n", foo?);
  format-out("Quux:            %=\n", quux);

  format-out("Warnings:\n");  
  for (arg in warnings)
    format-out("  <%s>\n", arg);
  end for;

  format-out("Defines:\n");
  for (key in key-sequence(defines))
    format-out("  <%s> = <%=>\n", key, defines[key]);
  end for;

  format-out("Regular arguments:\n");  
  for (arg in *parser*.regular-arguments)
    format-out("  <%s>\n", arg);
  end for;

  exit(exit-code: 0);
end method main;



Thu, 07 Jun 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Argument parsing in C module with variable number of arguments

2. Command-line argument parsing library?

3. effective argument parsing ?

4. Generic Argument Parsing Proc

5. Keyword argument parsing in error in 2.0?

6. Calling Parse-Arguments Library?

7. Regina REXX - How to Parse Arguments Containing Spaces

8. PARSE QUOTED (was Re: spaces in arguments)

9. parsing arguments

10. Parsing argument from an url

11. argument type checking before parsing

12. Parsing command line arguments

 

 
Powered by phpBB® Forum Software