pike.git / lib / modules / Getopt.pmod

version» Context lines:

pike.git/lib/modules/Getopt.pmod:87:   //! @[Getopt.get_args()]   //!   string|int find_option(array(string) argv,    array(string)|string shortform,    array(string)|string|void longform,    array(string)|string|void envvars,    string|int|void def,    int|void throw_errors)   {    string|int value; -  int i,hasarg; +     -  hasarg=query_num_arg() > 4; -  if(!arrayp(longform)) longform=({[string]longform}); -  if(!arrayp(shortform)) shortform=({[string]shortform}); -  if(!arrayp(envvars)) envvars=({[string]envvars}); +  int(0..1) hasarg = query_num_arg() > 4; +  if(!arrayp(longform)) longform = ({ [string]longform }); +  if(!arrayp(shortform)) shortform = ({ [string]shortform }); +  if(!arrayp(envvars)) envvars = ({ [string]envvars });    -  for(i=1; i<sizeof(argv); i++) +  for(int i=1; i<sizeof(argv); i++)    {    if(argv[i] && strlen(argv[i]) > 1)    {    if(argv[i][0] == '-')    {    if(argv[i][1] == '-')    {    string tmp;    int nf;   
pike.git/lib/modules/Getopt.pmod:187:    }       if(arrayp(envvars))    foreach([array(string)]envvars, value)    if(value && (value=[string]getenv([string]value)))    return value;       return def;   }    - /* -  * ({ "name", type, ({aliases}), env_var, default }) -  */ -  +    //!   constant HAS_ARG=1;      //!   constant NO_ARG=2;      //!   constant MAY_HAVE_ARG=3;    -  +  + // ({ "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 +    //! 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   //! the second argument to your @tt{main()@} function.
pike.git/lib/modules/Getopt.pmod:244:   //! can be combined into one string. Note that you must include the   //! dashes so that @[find_all_options()] can distinguish between   //! long and short options. Example: @tt{({"-tT","--test"})@}   //! This would make @[find_all_options] look for @tt{-t@},   //! @tt{-T@} and @tt{--test@}.   //! @elem void|string|array(string) env_var   //! This is a string or an array of strings containing names of   //! environment variables that can be used instead of the   //! command line option.   //! @elem void|mixed default - //! This is the default value the option will have in the output - //! from this function. Options without defaults will be omitted - //! from the output if they are not found in argv. + //! This is the default value a @[MAY_HAVE_ARG] option will have in the + //! output if it was set but not assign any value.   //! @endarray   //!   //! Only the first three elements need to be included.   //!   //! @param posix_me_harder   //! Don't scan for arguments after the first non-option.   //!   //! @param throw_errors   //! If @[throw_errors] has been specified @[find_all_options()] will throw   //! errors on failure. If it has been left out, or is @tt{0@} (zero), it will
pike.git/lib/modules/Getopt.pmod:281:   //!   //! @note   //! @[find_all_options()] modifies @[argv].   //!   //! Index @tt{0@} (zero) of @[argv] is not scanned for options, since it   //! is reserved for the program name.   //!   //! @seealso   //! @[Getopt.get_args()], @[Getopt.find_option()]   //! - array find_all_options(array(string) argv, + array(array) find_all_options(array(string) argv,    array(array(array(string)|string)) options,    void|int posix_me_harder, void|int throw_errors)   {    mapping(string:array(string|array(string))) quick=([]); -  foreach(options, array(array(string)|string) opt) -  { +  +  for(int i; i<sizeof(options); i++) { +  options[i] = options[i] + allocate(SIZE-sizeof(options[i])); +  array(array(string)|string) opt = options[i];    array(string)|string aliases=[array(string)|string]opt[ALIASES];    if(!arrayp(aliases)) aliases=({[string]aliases}); -  +     foreach([array(string)]aliases, string optname) -  { +     if(optname[0..1]=="--") -  { +     quick[optname]=opt; -  }else{ -  foreach(optname[1..]/"",string optletter) -  { +  else +  foreach(optname[1..]/1,string optletter)    quick["-"+optletter]=opt;    } -  } -  } -  } +     -  array ret=({}); -  for(int e=1;e<sizeof(argv);e++) +  array(array) ret=({}); +  for(int e=1; e<sizeof(argv); e++)    {    if(!argv[e]) continue;       if(strlen(argv[e]) && argv[e][0]=='-')    {    if(strlen(argv[e])>1 && argv[e][1]=='-')    {    string opt=argv[e];    if(opt=="--") break;   
pike.git/lib/modules/Getopt.pmod:335:    if (throw_errors) {    error( "No argument to option "+opt+".\n" );    } else {    werror("No argument to option "+opt+".\n");    exit(1);    }    }    arg=argv[e+1];    argv[e+1]=0;    } -  ret+=({ ({ option[0], arg || 1 }) }); +  ret+=({ ({ option[0], arg || option[DEF] || 1 }) });    } -  }else{ +  } +  else {    array(string) foo=argv[e]/"";    for(int j=1;j<strlen(foo);j++)    {    string opt="-"+foo[j];    if(array option=quick[opt])    {    foo[j]=0;    string arg;    if(option[TYPE]!=NO_ARG)    {
pike.git/lib/modules/Getopt.pmod:363:    {    if (throw_errors) {    error( "No argument to option "+opt+".\n" );    } else {    werror("No argument to option "+opt+".\n");    exit(1);    }    }    arg=argv[e+1];    argv[e+1]=0; -  }else{ +  } +  else    foo=foo[..j];    } -  } +        if (arg == "") arg = 0; -  ret+=({ ({ option[0], arg || 1 }) }); +  ret+=({ ({ option[0], arg || option[DEF] || 1 }) });    }    }    argv[e]=foo*"";    if(argv[e]=="-") argv[e]=0;    } -  }else{ +  } +  else{    if(posix_me_harder != -1)    if(posix_me_harder || getenv("POSIX_ME_HARDER"))    break;    }    }       multiset done=mkmultiset(column(ret, 0));    foreach(options, array(string|array(string)) option)    {    string name=[string]option[NAME];    if(done[name]) continue; -  if(sizeof(option) > ENV) +  if(option[ENV])    {    array(string)|string foo=option[ENV];    if(!foo) continue; -  if(stringp(foo)) foo=({[string]foo}); +  if(stringp(foo)) foo = ({ [string]foo });    foreach([array(string)]foo, foo)    {    if(foo=[string]getenv([string]foo))    { -  ret+=({ ({name, foo}) }); -  done[name]=1; +  ret += ({ ({name, foo}) }); +  done[name] = 1;    break;    }    }    -  if(!done && sizeof(option)>3 && option[3]) +  if(!done && option[ENV])    { -  ret+=({ ({name, option[3]}) }); -  done[name]=1; +  ret += ({ ({name, option[ENV]}) }); +  done[name] = 1;    }    }    } -  +     return ret;   }      //! This function returns the remaining command line arguments after   //! you have run @[find_option()] or @[find_all_options()] to find   //! all the options in the argument list. If there are any options   //! left not handled by @[find_option()] or @[find_all_options()]   //! this function will fail.   //!   //! If @[throw_errors] has been specified @[get_args()] will throw errors
pike.git/lib/modules/Getopt.pmod:479:    if(posix_me_harder != -1)    if(posix_me_harder || getenv("POSIX_ME_HARDER"))    break;    }    }       argv-=({0,1});       return argv;   } -  +