libcm is a C development framework with an emphasis on audio signal processing applications.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cmPgmOpts.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmPgmOpts_h
  4. #define cmPgmOpts_h
  5. //( { file_desc:"Command line argument description and parsing API." kw:[base]}
  6. //
  7. // Command line program option syntax:
  8. //
  9. //
  10. // '-'<charId>* <value>+ - a dash followed by one or more one character id's optionally followed by a parameter value.
  11. // '--'<wordId> <value>+ - a double dash followed by a word id optionally followed by a parameter value.
  12. //
  13. // A char id is a single character.
  14. // A word id is a string of characters with no intervening white space.
  15. //
  16. // Notes:
  17. // 1) There is no way to give multiple <values> without an intervening character or word id.
  18. // A <value> must therefore always be immediately preceded by an id.
  19. // 2) There must never be a space between the dash(es) and the characters forming the identifier.
  20. // 3) There must always be a space between the identifier and any subsequent <value>.
  21. // 4) See src/mas/src/main.c for a complete example.
  22. //
  23. // Terms:
  24. // Parameter - Description of the allowable types and constraints for a program option.
  25. // Argument - An instance of a parameter or the values associated with a parameter.
  26. //
  27. //)
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //(
  32. // cmPgmOpts Result Codes
  33. enum
  34. {
  35. kOkPoRC = cmOkRC,
  36. kSyntaxErrPoRC,
  37. kInvalidIdxPoRC,
  38. kParseFailPoRC,
  39. kNoReqArgPoRC,
  40. kDuplicateIdPoRC,
  41. kArgCntErrPoRC,
  42. kParmNotFoundPoRC,
  43. kInstNotFoundPoRC,
  44. kTypeErrPoRC
  45. };
  46. // cmPgmOpts parameter id's
  47. enum
  48. {
  49. kPrintHelpPoId,
  50. kVersionPoId,
  51. kPrintParmsPoId,
  52. kNoExecPoId,
  53. kBasePoId
  54. };
  55. typedef cmRC_t cmPoRC_t;
  56. typedef cmHandle_t cmPgmOptH_t;
  57. extern cmPgmOptH_t cmPgmOptNullHandle;
  58. // Initialize a program options parser.
  59. cmPoRC_t cmPgmOptInitialize(cmCtx_t* c, cmPgmOptH_t* hp, const cmChar_t* helpBegStr, const cmChar_t* helpEndStr );
  60. // Finalize a program options parser.
  61. cmPoRC_t cmPgmOptFinalize( cmPgmOptH_t* hp );
  62. // Return true if the program options parser was successfully initialized.
  63. bool cmPgmOptIsValid( cmPgmOptH_t h );
  64. // Flag used by the 'flags' arg. to cmPgmOptInstall().
  65. enum {
  66. kReqPoFl = 0x01, // this is a required parameter
  67. kHexPoFl = 0x02 // this integer must be given in hexidecimal or output an integer in hex.
  68. };
  69. // Define a parameter.
  70. //
  71. // unsigned numId, - Numeric id used to identify this parameter. The min. numId should be kPoBaseId.
  72. // const cmChar_t charId, - A character used to identify this parameter.
  73. // const cmChar_t* wordId, - A label used to identify this parameter.
  74. // unsigned flags, - kNoPoFlags | kReqPoFl (the type flags are automatically assigned)
  75. // unsigned enumId, - non-zero value used to group enumerated parameter values (ignored for non-enum types)
  76. // dfltVal - The default value for this parameter.
  77. // retValPtr- Optional pointer to a variable to receive the argument value for this parameter.
  78. // unsigned cnt, - count of times this parameter may repeated or 0 for an unlimited repetitions
  79. // const cmChar_t* helpStr - a textual description of this parameter
  80. //
  81. // Notes
  82. // 1) 'numId','charId', and 'wordId' must all be unique among all parameter definitions.
  83. // An error will be generated if they are not.
  84. // 2) For all parameter value types except the string type arguments are automatically parsed to the
  85. // defined type. To avoid automatic parsing simply define the type as a string (using cmPgmOptInstallStr()).
  86. // 3) All expected parameters must be defined prior to calling cmPgmOptParse().
  87. // 4) One call to cmPgmOptInstallEnum() is made for each possible enumeration value - where the 'enumId' gives the value.
  88. // A given set of associated enum values is grouped by giving a common 'numId'.
  89. // Include a master help string in one of the enumerated elements to give documentation
  90. // text for the entire set of values.
  91. // Example:
  92. // cmPgmOptInstallEnum(h,colorId,...,redId,...,"Red","Select a color");
  93. // cmPgmOptInstallEnum(h,colorId,...,greenId,..."Green",NULL);
  94. // cmPgmOptInstallEnum(h,colorId,...,blueId,...,"Blue",NULL);
  95. //
  96. // 5) The following id's are used for built-in actions and are therefore restricted from use by the client:
  97. // a. -h --help Print the program usage information.
  98. // b. -v --version Print the program version informatoin.
  99. // c. -p --parms Print the program parameter values.
  100. //
  101. // 6) If a retValPtr is specified then *retValPtr it is assigned 'dfltVal' as part of the
  102. // call to cmPgmOptInstXXX().
  103. //
  104. // 7) The default value of 'Flag' type parameters is always zero.
  105. // If the 'char' or 'word' id of the Flag parameter appears in the
  106. // actual argument list then the value of the argument is 'onValue'.
  107. // Unlike other parameters Flag parameters do not initialize *regValPtr.
  108. // If the retValPtr is given and the flag is set in the arg. list then
  109. // the retValPtr is set by bitwise assignment (i.e. *retValPtr |= dfltFlagValue).
  110. // This allows multiple Flag parameters to use the same retValPtr and
  111. // set independent bit fields in it.
  112. cmPoRC_t cmPgmOptInstallChar(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, cmChar_t dfltVal, cmChar_t* retValPtr, unsigned cnt, const cmChar_t* helpStr );
  113. cmPoRC_t cmPgmOptInstallBool(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, bool dfltVal, bool* retValPtr, unsigned cnt, const cmChar_t* helpStr );
  114. cmPoRC_t cmPgmOptInstallInt( cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, int dfltVal, int* retValPtr, unsigned cnt, const cmChar_t* helpStr );
  115. cmPoRC_t cmPgmOptInstallUInt(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, unsigned dfltVal, unsigned* retValPtr, unsigned cnt, const cmChar_t* helpStr );
  116. cmPoRC_t cmPgmOptInstallDbl( cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, double dfltVal, double* retValPtr, unsigned cnt, const cmChar_t* helpStr );
  117. cmPoRC_t cmPgmOptInstallStr( cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, const cmChar_t* dfltVal, const cmChar_t** retValPtr, unsigned cnt, const cmChar_t* helpStr );
  118. cmPoRC_t cmPgmOptInstallEnum(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, unsigned enumId, unsigned dfltVal, unsigned* retValPtr, unsigned cnt, const cmChar_t* helpStr, const cmChar_t* mstrHelpStr );
  119. cmPoRC_t cmPgmOptInstallFlag(cmPgmOptH_t h, unsigned numId, cmChar_t charId, const cmChar_t* worldId, unsigned flags, unsigned onValue, unsigned* retValPtr, unsigned cnt, const cmChar_t* helpStr );
  120. // Parse a set of command line arguments.
  121. //
  122. // 1) If only built-in parameters were specified then the NO check is done
  123. // to verify that required arguments were provided. However, if any non-built-in
  124. // arguments are provided then a check is performed to be sure that any
  125. // parameters specified with the kPoReqFl have associated argument values.
  126. //
  127. // 2) If a parameter was specified with a 'retValPtr' then *retValPtr is
  128. // set to the value of the last argument associated with the given parameter.
  129. // This means that 'retValPtr' is generally only useful when the
  130. // parameter instance count limit (the 'cnt' param to cmPgmOptInstallXXX())
  131. // was set to 1.
  132. //
  133. //
  134. cmPoRC_t cmPgmOptParse( cmPgmOptH_t h, unsigned argCnt, char* argArray[] );
  135. // Get the total count of arguments passed to cmPgmOptParse().
  136. unsigned cmPgmOptArgCount( cmPgmOptH_t h);
  137. // Get the numeric id associated with each argument.
  138. unsigned cmPgmOptNumId( cmPgmOptH_t h, unsigned argIdx );
  139. // Get the character id associated with this argument.
  140. unsigned cmPgmOptCharId( cmPgmOptH_t h, unsigned argIdx );
  141. // Get the word id associated with this argument.
  142. const cmChar_t* cmPgmOptWordId( cmPgmOptH_t h, unsigned argIdx );
  143. // Manually convert each argument string into the specified type.
  144. // These functions are useful if all of the parameters were defined using cmPgmOptInstallStr().
  145. // Use cmPgmOptRC() to check for errors.
  146. char cmPgmOptParseArgChar(cmPgmOptH_t h, unsigned argIdx );
  147. bool cmPgmOptParseArgBool(cmPgmOptH_t h, unsigned argIdx );
  148. int cmPgmOptParseArgInt( cmPgmOptH_t h, unsigned argIdx );
  149. unsigned cmPgmOptParseArgUInt(cmPgmOptH_t h, unsigned argIdx );
  150. double cmPgmOptParseArgDbl( cmPgmOptH_t h, unsigned argIdx );
  151. const char* cmPgmOptParseArgStr( cmPgmOptH_t h, unsigned argIdx );
  152. // Get the count of arg's for a given parameter.
  153. unsigned cmPgmOptParmArgCount( cmPgmOptH_t h, unsigned numId );
  154. // Get the value associated with each parsed argument.
  155. // If no argument was given for the requested parameter
  156. // (cmPgmOptParmArgCount(numId)==0) and 'instIdx' == 0 then the default value is returned.
  157. // Use cmPgOptRC() to check for errors.
  158. //
  159. // The parameter identified by numId must has been defined by an earlier call to
  160. // cmPgmOptInstallChar() or this function
  161. char cmPgmOptArgChar( cmPgmOptH_t h, unsigned numId, unsigned instIdx );
  162. // No matter the type of the parameter it will be converted to a bool.
  163. bool cmPgmOptArgBool( cmPgmOptH_t h, unsigned numId, unsigned instIdx );
  164. // All types, except strings, are converted to type int. Doubles are rounded.
  165. int cmPgmOptArgInt( cmPgmOptH_t h, unsigned numId, unsigned instIdx );
  166. // All types, except strings, are converted to type unsigned. Doubles are rounded.
  167. unsigned cmPgmOptArgUInt( cmPgmOptH_t h, unsigned numId, unsigned instIdx );
  168. // All types except strings, are converted to double.
  169. double cmPgmOptArgDbl( cmPgmOptH_t h, unsigned numId, unsigned instIdx );
  170. // If the parameter is not defined as a string then the arg. string value us returned.
  171. const char* cmPgmOptArgStr( cmPgmOptH_t h, unsigned numId, unsigned instIdx );
  172. // Get and set the current result code.
  173. cmPoRC_t cmPgmOptRC( cmPgmOptH_t h, cmPoRC_t rc );
  174. // Returns 'false' if only built-in options were selected otherwise returns true.
  175. bool cmPgmOptHandleBuiltInActions( cmPgmOptH_t h, cmRpt_t* rpt );
  176. void cmPgmOptPrintHelp( cmPgmOptH_t h, cmRpt_t* rpt );
  177. void cmPgmOptPrintVersion( cmPgmOptH_t h, cmRpt_t* rpt );
  178. void cmPgmOptPrintParms( cmPgmOptH_t h, cmRpt_t* rpt );
  179. //)
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif