pike.git / lib / modules / Getopt.pmod

version» Context lines:

pike.git/lib/modules/Getopt.pmod:9:   //! consists of a dash followed by a character (@tt{-t@}), the long ones   //! consist of two dashes followed by a string of text (@tt{--test@}).   //! The short options can also be combined, which means that you can write   //! @tt{-tda@} instead of @tt{-t -d -a@}.   //!   //! Options can also require arguments, in which case they cannot be   //! combined. To write an option with an argument you write   //! @tt{-t @i{argument@}@} or @tt{-t@i{argument@}@} or   //! @tt{--test=@i{argument@}@}.    + static void my_error(string err, int throw_errors) { +  if(throw_errors) error(err); +  werror(err); +  exit(1); + }      //! This is a generic function to parse command line options of the   //! type @tt{-f@}, @tt{--foo@} or @tt{--foo=bar@}.   //!   //! @param argv   //! The first argument should be the array of strings that was sent as   //! the second argument to your @tt{main()@} function.   //!   //! @param shortform   //! The second is a string with the short form of your option. The
pike.git/lib/modules/Getopt.pmod:42:   //! to specify the same option, to make it easier to customize   //! program usage. It can also be an array of strings, in which case   //! any of the mentioned variables in the array may be used.   //!   //! @param def   //! This argument has two functions: It specifies if the option takes an   //! argument or not, and it informs @[find_option()] what to return if the   //! option is not present. If @[def] is given and the option does not have an   //! argument @[find_option()] will fail.   //! + //! @param throw_errors + //! If @[throw_errors] has been specified @[find_option()] will throw + //! errors on failure. If it has been left out, or is @tt{0@} (zero), it will + //! instead print an error message on @[Stdio.stderr] and exit the + //! program with result code 1 on failure. + //!   //! @returns   //! Returns the value the option has been set to if any.   //!   //! If the option is present, but has not been set to anything @tt{1@}   //! will be returned.   //!   //! Otherwise if any of the environment variables specified in @[envvars] has   //! been set, that value will be return.   //!   //! If all else fails, @[def] will be returned.   //!   //! @throws - //! If an option that requires an argument misses the argument an - //! error will be thrown. + //! If an option that requires an argument misses the argument and + //! @[throw_errors] is set an error will be thrown.   //!   //! @note   //! @[find_option()] modifies @[argv].   //!   //! This function reads options even if they are written after the first   //! non-option on the line.   //!   //! Index @tt{0@} (zero) of @[argv] is not scanned for options, since it   //! is reserved for the program name.   //!   //! @seealso   //! @[Getopt.get_args()]   //!   string|int(0..1) find_option(array(string) argv,    array(string)|string shortform,    array(string)|string|void longform,    array(string)|string|void envvars, -  string|int(0..1)|void def) +  string|int(0..1)|void def, +  int|void throw_errors)   {    string|int(0..1) value;    -  int(0..1) hasarg = query_num_arg() > 4; +  int(0..1) hasarg = !zero_type(def);    if(!arrayp(longform)) longform = ({ [string]longform });    if(!arrayp(shortform)) shortform = ({ [string]shortform });    if(stringp(envvars)) envvars = ({ [string]envvars });       foreach(argv; int i; string opt) {    if(!i || !opt || sizeof(opt)<2 || opt[0]!='-') continue;       if(opt[1] == '-') {    if(opt=="--") break;       string tmp=opt;    sscanf(tmp, "%s=%s", tmp, value);       if(has_value([array(string)]longform, tmp[2..])) {    argv[i]=0;       if(hasarg && !value) {    if(i == sizeof(argv)-1) -  error( "No argument to option "+tmp+".\n" ); +  my_error( "No argument to option "+tmp+".\n",throw_errors );       value=argv[i+1];    argv[i+1]=0;    }       return value || 1;    }    }    else {    foreach(opt/1; int j; string sopt) {       if(has_value([array(string)]shortform, sopt)) {    string arg = opt[j+1..];       if(hasarg) {    if(arg == "") {    if(i == sizeof(argv)-1) -  error( "No argument to option -"+sopt+".\n" ); +  my_error( "No argument to option -"+sopt+".\n",throw_errors );       value=argv[i+1];    argv[i+1] = 0;    }    else {    value=arg;    arg="";    }    }    else
pike.git/lib/modules/Getopt.pmod:163:   // ({ "name", type, "alias"|({"aliases"}), "env_var", default })      #define NAME 0   #define TYPE 1   #define ALIASES 2   #define ENV 3   #define DEF 4      #define SIZE 5    - static void my_error(string err, int throw_errors) { -  if(throw_errors) error(err); -  werror(err); -  exit(1); - } +       //! This function does the job of several calls to @[find_option()].   //! The main advantage of this is that it allows it to handle the   //! @tt{@b{POSIX_ME_HARDER@}@} environment variable better. When the either   //! the argument @[posix_me_harder] or the environment variable   //! @tt{@b{POSIX_ME_HARDER@}@} is true, no arguments will be parsed after   //! the first non-option on the command line.   //!   //! @param argv   //! The should be the array of strings that was sent as