Command-line arguments: Difference between revisions

Content added Content deleted
m (Improve shell code for calling a lang program)
m (→‎Using the Arg module: update link/formatting)
Line 1,810: Line 1,810:
done</syntaxhighlight>
done</syntaxhighlight>


=== Using the [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Arg.html Arg] module ===
=== Using the [https://ocaml.org/api/Arg.html Arg] module ===

<syntaxhighlight lang="ocaml">(* default values *)
<syntaxhighlight lang="ocaml">(* default values *)
let somebool = ref false
let somebool = ref false
Line 1,831: Line 1,830:
(fun x -> raise (Arg.Bad ("Bad argument : " ^ x)))
(fun x -> raise (Arg.Bad ("Bad argument : " ^ x)))
usage;
usage;
Printf.printf " %b %d '%s'\n" !somebool !someint !somestr</syntaxhighlight>
<pre>
$ ocaml arg.ml --help
usage: arg.ml [-b] [-s string] [-d int]
-b : set somebool to true
-s : what follows -s sets some string
-d : some int parameter
-help Display this list of options
--help Display this list of options


$ ocaml arg.ml -d 4 -b -s blabla
Printf.printf " %b %d '%s'\n" !somebool !someint !somestr;
true 4 'blabla'
;;</syntaxhighlight>



% ocaml arg.ml --help
$ ocaml arg.ml
false 0 ''
usage: tmp.ml [-b] [-s string] [-d int]
</pre>
-b : set somebool to true
-s : what follows -s sets some string
-d : some int parameter
--help Display this list of options
% ocaml arg.ml -d 4 -b -s blabla
true 4 'blabla'
% ocaml arg.ml
false 0 <nowiki>''</nowiki>


=={{header|Odin}}==
=={{header|Odin}}==