hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

README (2480B)


      1 getopt provides an interface for parsing command line arguments and
      2 automatically generates a brief help message explaining the command usage. See
      3 [[parse]] or [[tryparse]] for the main entry point.
      4 
      5 The caller provides [[help]] arguments to specify which command line flags and
      6 parameters are supported, and to provide some brief help text which describes
      7 their use. Provide [[flag_help]] to add a flag which does not take a parameter,
      8 and [[parameter_help]] to add a flag with a required parameter. The first
      9 [[cmd_help]] is used as a short, one-line summary of the command's purpose, and
     10 any later [[cmd_help]] arguments are used to provide the name of any arguments
     11 which follow the options list.
     12 
     13 By convention, the caller should sort the list of options, first providing all
     14 flags, then all parameters, alpha-sorted within each group by the flag rune.
     15 
     16 	// Usage for sed
     17 	const cmd = getopt::parse(os::args,
     18 		"stream editor",
     19 		('E', "use extended regular expressions"),
     20 		('i', "edit files in place"),
     21 		('s', "treat files as separate, rather than one continuous stream"),
     22 		('z', "separate lines by NUL characters"),
     23 		('e', "script", "execute commands from script"),
     24 		('f', "file", "execute commands from a file"),
     25 		"files...",
     26 	);
     27 	defer getopt::finish(&cmd);
     28 
     29 	for (let opt .. cmd.opts) {
     30 		switch (opt.0) {
     31 		case 'E' =>
     32 			extended = true;
     33 		case 'i' =>
     34 			in_place = true;
     35 		case 's' =>
     36 			continuous = false;
     37 		case 'z' =>
     38 			sep = '\0';
     39 		case 'e' =>
     40 			script = opt.1;
     41 		case 'f' =>
     42 			file = opt.1;
     43 		case => abort(); // unreachable
     44 		};
     45 	};
     46 
     47 	for (let arg .. cmd.args) {
     48 		// ...
     49 	};
     50 
     51 If any non-option arguments are provided on the command line, the options list
     52 is terminated by the first non-option argument, or by a "--" argument if one is
     53 present. The arguments list is NOT reordered to accomodate for options which
     54 appear after other arguments. Overriding the behavior of "--" is not supported;
     55 providing '-' as a flag or parameter rune will have no effect.
     56 
     57 If "-h" is not among the options defined by the caller, the "-h" option will
     58 cause a summary of the command usage to be printed to [[os::stderr]] (see also
     59 [[printhelp]]), and [[os::exit]] will be called with [[os::status::SUCCESS]].
     60 The help text is brief and should serve only as a reminder. It is recommended
     61 that your command line program be accompanied by a man page to provide detailed
     62 usage information. 'h' may be provided as a flag or parameter rune to override
     63 the default behavior.