caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Use Arg module to read keyword followed by unspecified number of arguments
@ 2006-05-23 17:17 mulhern
  2006-05-23 17:29 ` [Caml-list] " Till Varoquaux
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: mulhern @ 2006-05-23 17:17 UTC (permalink / raw)
  To: caml-list

Hi!

I expect to be reading something like this:

-pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
<arg2> ... <argn> -key4

There may be 0 or more <args> specified after -pred-files and after
-extra-files. I need to treat the args differently depending on what
keyword they are associated with.

I can think of about 10 ways to do this using the Arg module, but
they're all ugly and all different. Does anybody have an attractive
solution to this problem or is aware of the accepted way to handle
this situation? An example in some existing code would be good enough.

I do not consider changing the call to
-pred-files <arg1> -pred-files <arg2> ... -pred-files <argn> ......
an option at this time, principally because the call is automatically
generated and I'ld like it to stay in line with the other
automatically generated calls in the same distribution.

Thanks!

-mulhern


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

* Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
  2006-05-23 17:17 Use Arg module to read keyword followed by unspecified number of arguments mulhern
@ 2006-05-23 17:29 ` Till Varoquaux
  2006-05-23 17:49 ` Oliver Bandel
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Till Varoquaux @ 2006-05-23 17:29 UTC (permalink / raw)
  To: mulhern; +Cc: caml-list

You could work around with references but I guess you'd consider this
as ugly. A comma separated list of strings could do the trick:
 "-pred-files", Arg.String (fun s -> predfiles:=Str.split
(Str.regexp_string ",") s),"...")

cheers,
Till
On 5/23/06, mulhern <mulhern@gmail.com> wrote:
> Hi!
>
> I expect to be reading something like this:
>
> -pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
> <arg2> ... <argn> -key4
>
> There may be 0 or more <args> specified after -pred-files and after
> -extra-files. I need to treat the args differently depending on what
> keyword they are associated with.
>
> I can think of about 10 ways to do this using the Arg module, but
> they're all ugly and all different. Does anybody have an attractive
> solution to this problem or is aware of the accepted way to handle
> this situation? An example in some existing code would be good enough.
>
> I do not consider changing the call to
> -pred-files <arg1> -pred-files <arg2> ... -pred-files <argn> ......
> an option at this time, principally because the call is automatically
> generated and I'ld like it to stay in line with the other
> automatically generated calls in the same distribution.
>
> Thanks!
>
> -mulhern
>
> _______________________________________________
> 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] 8+ messages in thread

* Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
  2006-05-23 17:17 Use Arg module to read keyword followed by unspecified number of arguments mulhern
  2006-05-23 17:29 ` [Caml-list] " Till Varoquaux
@ 2006-05-23 17:49 ` Oliver Bandel
  2006-05-23 17:55 ` Oliver Bandel
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Oliver Bandel @ 2006-05-23 17:49 UTC (permalink / raw)
  To: caml-list

On Tue, May 23, 2006 at 12:17:18PM -0500, mulhern wrote:
> Hi!
> 
> I expect to be reading something like this:
> 
> -pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
> <arg2> ... <argn> -key4
> 
> There may be 0 or more <args> specified after -pred-files and after
> -extra-files. I need to treat the args differently depending on what
> keyword they are associated with.

Do you need a keyword for these pred-files arg's?

Why don't you use un-named (anonyme) args?

It looks like a filelist.

The last anonymous function ( "(fun name -> if Sy.file_exists name"(...) )
will be called for all args that are givewn, without a switch that
specifies which kind of arg will follow.


Example:

let parse () = (*print_endline "OK, starting the parse!";*)
    Arg.parse [
         ("-dp", Arg.String (fun x -> dirnameopt := Prefix x), " <dirpref> directory name-prefix");
         ("-fd", Arg.String (fun x -> dirnameopt := Fixed x), "<fixdir> fixed directory name");
         ("-inv", Arg.Unit (fun () -> filter_selection := Inverted ), " invert the template filter ... <template>" );
         ("-ad", Arg.Unit (fun () -> fk_sel := Allow_dirs ), " allow dirs to be moved ...!" );
         ("-v", Arg.Unit (fun () -> Printf.printf "%s\n" version;exit 0), " version of the program" )
              ] 
         ( fun name -> if Sys.file_exists name then Hashtbl.replace filenames name "" ) 
         "Use the program with following options:"




Hope, this helps.



Ciao,
   Oliver


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

* Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
  2006-05-23 17:17 Use Arg module to read keyword followed by unspecified number of arguments mulhern
  2006-05-23 17:29 ` [Caml-list] " Till Varoquaux
  2006-05-23 17:49 ` Oliver Bandel
@ 2006-05-23 17:55 ` Oliver Bandel
  2006-05-23 19:21 ` Michael Wohlwend
  2006-05-24 14:03 ` Richard Jones
  4 siblings, 0 replies; 8+ messages in thread
From: Oliver Bandel @ 2006-05-23 17:55 UTC (permalink / raw)
  To: caml-list

On Tue, May 23, 2006 at 12:17:18PM -0500, mulhern wrote:
> Hi!
> 
> I expect to be reading something like this:
> 
> -pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
> <arg2> ... <argn> -key4
> 
> There may be 0 or more <args> specified after -pred-files and after
> -extra-files. I need to treat the args differently depending on what
> keyword they are associated with.

You could maybe change what args you want to have.
If one arglist with arbitrary number of args would be sufficient,
you could do it with anonymous args, as I stated in my last mail.

If this does not work, you maybe should parse Sys.argv by your own.

Ciao,
   Oliver


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

* Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
  2006-05-23 17:17 Use Arg module to read keyword followed by unspecified number of arguments mulhern
                   ` (2 preceding siblings ...)
  2006-05-23 17:55 ` Oliver Bandel
@ 2006-05-23 19:21 ` Michael Wohlwend
  2006-05-23 19:50   ` Till Varoquaux
  2006-05-24 14:03 ` Richard Jones
  4 siblings, 1 reply; 8+ messages in thread
From: Michael Wohlwend @ 2006-05-23 19:21 UTC (permalink / raw)
  To: caml-list; +Cc: mulhern

On Tuesday 23 May 2006 19:17, mulhern wrote:
> Hi!
>
> I expect to be reading something like this:
>
> -pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
> <arg2> ... <argn> -key4

I think the idea by Till to separate the args with commas is best, but if you 
don't want to do this, maybe something like that would be o.k. :

let mode_pred = ref true;;
let pred_files = ref [];;
let extra_files = ref [];;
let key2 = ref "";;

Arg.parse [
	"-extra-files", Arg.Clear mode_pred, "extra files";
	"-key2", Arg.Set_string key2, "key2";
	"-pred-files", Arg.Set mode_pred, "pred files"
	]
	(fun name -> if !mode_pred then
		 	pred_files := name :: !pred_files
		else
			extra_files := name :: !extra_files
	) "usage..."
;;


cheers,
 Michael


-- 
C offers you enough rope to hang yourself.
C++ offers a fully equipped firing squad, a last cigarette and
a blindfold.


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

* Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
  2006-05-23 19:21 ` Michael Wohlwend
@ 2006-05-23 19:50   ` Till Varoquaux
  2006-05-23 19:57     ` Michael Wohlwend
  0 siblings, 1 reply; 8+ messages in thread
From: Till Varoquaux @ 2006-05-23 19:50 UTC (permalink / raw)
  To: Michael Wohlwend; +Cc: caml-list, mulhern

Actualy I'd stick with double references... (yuck). From the top of my head:
let a=ref [];;
let b=ref [];;
let c=ref a;;

let p_list l=
        List.iter (Printf.printf "\"%s\"") l;
        print_newline ()

let _=
        Arg.parse [("-switch",Arg.Unit (fun () -> c:=b),"")]
                (fun s -> !c:=s::!(!c))
                "blah";
        p_list !a;
        p_list !b

Note how c is a double reference...

Till

On 5/23/06, Michael Wohlwend <micha-1@fantasymail.de> wrote:
> On Tuesday 23 May 2006 19:17, mulhern wrote:
> > Hi!
> >
> > I expect to be reading something like this:
> >
> > -pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
> > <arg2> ... <argn> -key4
>
> I think the idea by Till to separate the args with commas is best, but if you
> don't want to do this, maybe something like that would be o.k. :
>
> let mode_pred = ref true;;
> let pred_files = ref [];;
> let extra_files = ref [];;
> let key2 = ref "";;
>
> Arg.parse [
>         "-extra-files", Arg.Clear mode_pred, "extra files";
>         "-key2", Arg.Set_string key2, "key2";
>         "-pred-files", Arg.Set mode_pred, "pred files"
>         ]
>         (fun name -> if !mode_pred then
>                         pred_files := name :: !pred_files
>                 else
>                         extra_files := name :: !extra_files
>         ) "usage..."
> ;;
>
>
> cheers,
>  Michael
>
>
> --
> C offers you enough rope to hang yourself.
> C++ offers a fully equipped firing squad, a last cigarette and
> a blindfold.
>
> _______________________________________________
> 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] 8+ messages in thread

* Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
  2006-05-23 19:50   ` Till Varoquaux
@ 2006-05-23 19:57     ` Michael Wohlwend
  0 siblings, 0 replies; 8+ messages in thread
From: Michael Wohlwend @ 2006-05-23 19:57 UTC (permalink / raw)
  To: Till Varoquaux; +Cc: caml-list

On Tuesday 23 May 2006 21:50, Till Varoquaux wrote:
> Actualy I'd stick with double references... (yuck). From the top of my

I like that  :-)

 Michael


--
C offers you enough rope to hang yourself.
C++ offers a fully equipped firing squad, a last cigarette and
a blindfold.


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

* Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
  2006-05-23 17:17 Use Arg module to read keyword followed by unspecified number of arguments mulhern
                   ` (3 preceding siblings ...)
  2006-05-23 19:21 ` Michael Wohlwend
@ 2006-05-24 14:03 ` Richard Jones
  4 siblings, 0 replies; 8+ messages in thread
From: Richard Jones @ 2006-05-24 14:03 UTC (permalink / raw)
  To: mulhern; +Cc: caml-list

On Tue, May 23, 2006 at 12:17:18PM -0500, mulhern wrote:
> Hi!
> 
> I expect to be reading something like this:
> 
> -pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
> <arg2> ... <argn> -key4

Have you thought about _not_ using the Arg module?  A loop with
pattern matching is often good enough, or in this case two nested
loops:

let pred_files = ref []
let key2 = ref ""
let extra_files = ref []
let key4 = ref ""

let rec read_args = function
  | "-pred-files" :: args ->
      let args, rest = read_to_next_switch args in
      pred_files := args;
      read_args rest
  | "-key2" :: arg :: rest ->
      key2 := arg;
      read_args rest
  | "-extra-files" :: args ->
      let args, rest = read_to_next_switch args in
      extra_files := args;
      read_args rest
  | "-key4" :: arg :: rest ->
      key4 := arg;
      read_args rest
  | [] -> ()
  | arg :: _ ->
      prerr_endline (arg ^ ": unknown parameter");
      exit 2

and read_to_next_switch = function
  | (switch :: _) as rest when String.length switch > 0 && switch.[0] = '-' ->
      [], rest
  | arg :: rest ->
      let args, rest = read_to_next_switch rest in
      arg :: args, rest
  | [] ->
      [], []
  ;;

read_args (List.tl (Array.to_list Sys.argv)) ;;

print_endline ("pred-files = " ^ String.concat ", " !pred_files) ;;
print_endline ("key2 = " ^ !key2) ;;
print_endline ("extra-files = " ^ String.concat ", " !extra_files) ;;
print_endline ("key4 = " ^ !key4) ;;

----------------------------------------------------------------------
$ ./parse -pred-files arg1 arg2 arg3 -key2 key2 -extra-files arg1 arg2 -key4 key4
pred-files = arg1, arg2, arg3
key2 = key2
extra-files = arg1, arg2
key4 = key4

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


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

end of thread, other threads:[~2006-05-24 14:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-23 17:17 Use Arg module to read keyword followed by unspecified number of arguments mulhern
2006-05-23 17:29 ` [Caml-list] " Till Varoquaux
2006-05-23 17:49 ` Oliver Bandel
2006-05-23 17:55 ` Oliver Bandel
2006-05-23 19:21 ` Michael Wohlwend
2006-05-23 19:50   ` Till Varoquaux
2006-05-23 19:57     ` Michael Wohlwend
2006-05-24 14:03 ` Richard Jones

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).