caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* 'Pass on' argument from Arg.parse to Arg.parse_argv
@ 2005-01-21 16:48 Richard Jones
  2005-01-22 11:05 ` [Caml-list] " Jon Harrop
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Jones @ 2005-01-21 16:48 UTC (permalink / raw)
  To: caml-list


I have a bunch of command-line programs which take a standard set of
arguments, so I wrote a module 'StdArg' which each program uses to
parse arguments.  That module provides an interface like this:

----------------------------------------------------------------------
val username : string
val password : string
val client : string option
(** Username, password, client passed on the command line (or defaults). *)

val update : bool
(** True if the [--update] flag was passed on the command line, instructing
  * the program to perform updates.
  *)

val verbose : bool
(** True if the [--verbose] flag was passed on the command line, instructing
  * the program to be verbose.
  *)

val args : string list
(** Remaining, unparsed arguments on the command line. *)
----------------------------------------------------------------------

Now, one of my programs requires an additional flag on the command
line.  The original idea was that this program could call:

  Arg.parse_argv args argspec [etc.]

but this unfortunately doesn't work, because the program doesn't get
beyond the StdArg call to Arg.parse before printing this error message
and exiting:

  ./prog: unknown option `--foobar'.
  [followed by usage message]

Is there a way to do this?  I'd like Arg.parse to ignore unknown args.

Rich.

-- 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] 'Pass on' argument from Arg.parse to Arg.parse_argv
  2005-01-21 16:48 'Pass on' argument from Arg.parse to Arg.parse_argv Richard Jones
@ 2005-01-22 11:05 ` Jon Harrop
  2005-01-22 16:29   ` Martin Willensdorfer
  0 siblings, 1 reply; 3+ messages in thread
From: Jon Harrop @ 2005-01-22 11:05 UTC (permalink / raw)
  To: caml-list

On Friday 21 January 2005 16:48, Richard Jones wrote:
> ...
> but this unfortunately doesn't work, because the program doesn't get
> beyond the StdArg call to Arg.parse before printing this error message
> and exiting:
>
>   ./prog: unknown option `--foobar'.
>   [followed by usage message]
>
> Is there a way to do this?  I'd like Arg.parse to ignore unknown args.

What about providing your StdArg module with a way to let you register new 
arguments and either let you specify how they will be parsed or parse them 
separately afterwards?

Cheers,
Jon.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] 'Pass on' argument from Arg.parse to Arg.parse_argv
  2005-01-22 11:05 ` [Caml-list] " Jon Harrop
@ 2005-01-22 16:29   ` Martin Willensdorfer
  0 siblings, 0 replies; 3+ messages in thread
From: Martin Willensdorfer @ 2005-01-22 16:29 UTC (permalink / raw)
  To: caml-list

> > Is there a way to do this?  I'd like Arg.parse to ignore unknown args.

I don't know if that helps you, but I had a similar situation. I also wanted 
to "reused" command line arguments. However, I could cramp everything into 
one module (config.ml). Below is an example showing you how I did it. 
config_1 uses its own flags and the flags defined in config_0. I am sure you 
can do something similar by replacing my config_0 with whatever you have in 
your StdArg module.

Other than that, is there a way to use the things defined for Arg.parse to 
read values from a file when the flag '-prm_file <filename>' is used. My list 
of command line arguments gets bigger and bigger and I would like to be able 
to use parameter files (for reproducibility and convenience) and to reuse the 
code written for command-line arguments.

Best wishes,

Martin

------------------------------------------------------ config.ml :

type config_0 = {
    a      : float;
  }

type config_1 = {
    prm_config_0 : config_0 ref;
    dimx : int;
  }

let default_config_0 = {
  a      = 300.;
}

let default_config_1 = {
  prm_config_0 = ref default_config_0;
  dimx = 10;
}

(* prm for config_0 *)
let set_a       cf value = cf := {!cf with a       = value};;

(* prm for config_1 *)
let set_dimx    cf value = cf := {!cf with dimx   = value};;

let speclist_config_0 cf =
  [("-a",      Arg.Float (set_a   cf)  , "commend")]

let speclist_config_1 cf =
  [("-dimx", Arg.Int   (set_dimx cf) , "x") ]@
 (speclist_config_0 !cf.prm_config_0)

let get_config_0_prm cf label =
  match label with
  | "a"      -> Float cf.a
  | _ -> failwith ("get_config_0_prm: cannot find "^label^" in config")
let get_config_1_prm cf label =
  match label with
  | "dimx" -> Int cf.dimx
  | _ -> try get_config_0_prm !(cf.prm_config_0) label with
      Failure message -> failwith ("get_config_0_prm: "^message)


------------------------------------------------------ usage :


let read_args () =
  let cf = ref Config.default_config_1 in
  let speclist = Config.speclist_config_1 cf in
  let usage_msg = "whatever" in
  Arg.parse speclist (fun s -> ()) usage_msg; !cf;;




On Saturday 22 January 2005 06:05, Jon Harrop wrote:
> On Friday 21 January 2005 16:48, Richard Jones wrote:
> > ...
> > but this unfortunately doesn't work, because the program doesn't get
> > beyond the StdArg call to Arg.parse before printing this error message
> > and exiting:
> >
> >   ./prog: unknown option `--foobar'.
> >   [followed by usage message]
> >
> > Is there a way to do this?  I'd like Arg.parse to ignore unknown args.
>
> What about providing your StdArg module with a way to let you register new
> arguments and either let you specify how they will be parsed or parse them
> separately afterwards?
>
> Cheers,
> Jon.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-01-22 16:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-21 16:48 'Pass on' argument from Arg.parse to Arg.parse_argv Richard Jones
2005-01-22 11:05 ` [Caml-list] " Jon Harrop
2005-01-22 16:29   ` Martin Willensdorfer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).