hare

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

README (2570B)


      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 i = 0z; i < len(cmd.opts); i += 1) {
     30 		const opt = cmd.opts[i];
     31 		switch (opt.0) {
     32 		case 'E' =>
     33 			extended = true;
     34 		case 'i' =>
     35 			in_place = true;
     36 		case 's' =>
     37 			continuous = false;
     38 		case 'z' =>
     39 			sep = '\0';
     40 		case 'e' =>
     41 			script = opt.1;
     42 		case 'f' =>
     43 			file = opt.1;
     44 		case => abort(); // unreachable
     45 		};
     46 	};
     47 
     48 	for (let i = 0z; i < len(cmd.args); i += 1) {
     49 		const arg = cmd.args[i];
     50 		// ...
     51 	};
     52 
     53 If any non-option arguments are provided on the command line, the options list
     54 is terminated by the first non-option argument, or by a "--" argument if one is
     55 present. The arguments list is NOT reordered to accomodate for options which
     56 appear after other arguments. Overriding the behavior of "--" is not supported;
     57 providing '-' as a flag or parameter rune will have no effect.
     58 
     59 If "-h" is not among the options defined by the caller, the "-h" option will
     60 cause a summary of the command usage to be printed to [[os::stderr]] (see also
     61 [[printhelp]]), and [[os::exit]] will be called with [[os::status::SUCCESS]].
     62 The help text is brief and should serve only as a reminder. It is recommended
     63 that your command line program be accompanied by a man page to provide detailed
     64 usage information. 'h' may be provided as a flag or parameter rune to override
     65 the default behavior.