plgetopt.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. ** File: plgetopt.h
  7. ** Description: utilities to parse argc/argv
  8. */
  9. #if defined(PLGETOPT_H_)
  10. #else
  11. #define PLGETOPT_H_
  12. #include "prtypes.h"
  13. PR_BEGIN_EXTERN_C
  14. typedef struct PLOptionInternal PLOptionInternal;
  15. typedef enum
  16. {
  17. PL_OPT_OK, /* all's well with the option */
  18. PL_OPT_EOL, /* end of options list */
  19. PL_OPT_BAD /* invalid option (and value) */
  20. } PLOptStatus;
  21. typedef struct PLLongOpt
  22. {
  23. const char * longOptName; /* long option name string */
  24. PRIntn longOption; /* value put in PLOptState for this option. */
  25. PRBool valueRequired; /* If option name not followed by '=', */
  26. /* value is the next argument from argv. */
  27. } PLLongOpt;
  28. typedef struct PLOptState
  29. {
  30. char option; /* the name of the option */
  31. const char *value; /* the value of that option | NULL */
  32. PLOptionInternal *internal; /* private processing state */
  33. PRIntn longOption; /* value from PLLongOpt put here */
  34. PRIntn longOptIndex; /* index into caller's array of PLLongOpts */
  35. } PLOptState;
  36. /*
  37. * PL_CreateOptState
  38. *
  39. * The argument "options" points to a string of single-character option
  40. * names. Option names that may have an option argument value must be
  41. * followed immediately by a ':' character.
  42. */
  43. PR_EXTERN(PLOptState*) PL_CreateOptState(
  44. PRIntn argc, char **argv, const char *options);
  45. /*
  46. * PL_CreateLongOptState
  47. *
  48. * Alternative to PL_CreateOptState.
  49. * Allows caller to specify BOTH a string of single-character option names,
  50. * AND an array of structures describing "long" (keyword) option names.
  51. * The array is terminated by a structure in which longOptName is NULL.
  52. * Long option values (arguments) may always be given as "--name=value".
  53. * If PLLongOpt.valueRequired is not PR_FALSE, and the option name was not
  54. * followed by '=' then the next argument from argv is taken as the value.
  55. */
  56. PR_EXTERN(PLOptState*) PL_CreateLongOptState(
  57. PRIntn argc, char **argv, const char *options,
  58. const PLLongOpt *longOpts);
  59. /*
  60. * PL_DestroyOptState
  61. *
  62. * Call this to destroy the PLOptState returned from PL_CreateOptState or
  63. * PL_CreateLongOptState.
  64. */
  65. PR_EXTERN(void) PL_DestroyOptState(PLOptState *opt);
  66. /*
  67. * PL_GetNextOpt
  68. *
  69. * When this function returns PL_OPT_OK,
  70. * - opt->option will hold the single-character option name that was parsed,
  71. * or zero.
  72. * When opt->option is zero, the token parsed was either a "long" (keyword)
  73. * option or a positional parameter.
  74. * For a positional parameter,
  75. * - opt->longOptIndex will contain -1, and
  76. * - opt->value will point to the positional parameter string.
  77. * For a long option name,
  78. * - opt->longOptIndex will contain the non-negative index of the
  79. * PLLongOpt structure in the caller's array of PLLongOpt structures
  80. * corresponding to the long option name, and
  81. * For a single-character or long option,
  82. * - opt->longOption will contain the value of the single-character option
  83. * name, or the value of the longOption from the PLLongOpt structure
  84. * for that long option. See notes below.
  85. * - opt->value will point to the argument option string, or will
  86. * be NULL if option does not require argument. If option requires
  87. * argument but it is not provided, PL_OPT_BAD is returned.
  88. * When opt->option is non-zero,
  89. * - opt->longOptIndex will be -1
  90. * When this function returns PL_OPT_EOL, or PL_OPT_BAD, the contents of
  91. * opt are undefined.
  92. *
  93. * Notes: It is possible to ignore opt->option, and always look at
  94. * opt->longOption instead. opt->longOption will contain the same value
  95. * as opt->option for single-character option names, and will contain the
  96. * value of longOption from the PLLongOpt structure for long option names.
  97. * This means that it is possible to equivalence long option names to
  98. * single character names by giving the longOption in the PLLongOpt struct
  99. * the same value as the single-character option name.
  100. * For long options that are NOT intended to be equivalent to any single-
  101. * character option, the longOption value should be chosen to not match
  102. * any possible single character name. It might be advisable to choose
  103. * longOption values greater than 0xff for such long options.
  104. */
  105. PR_EXTERN(PLOptStatus) PL_GetNextOpt(PLOptState *opt);
  106. PR_END_EXTERN_C
  107. #endif /* defined(PLGETOPT_H_) */
  108. /* plgetopt.h */