The results of parsing are stored by separating into command name, command arguments, options, and option arguments.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionargs()Returns the command arguments.booleanChecks whether an option with the specified name exists.name()Returns the command name.Returns the option argument with the specified name.Returns the option arguments with the specified name.optCfgs()Returns the option configurations which was used to parse command line arguments.voidparse()Parses command line arguments without configurations.voidParses command line arguments and set their option values to the fields of the option store which is passed as the argument of this method.Parses command line arguments without configurations but stops parsing when encountering first command argument.parseUntilSubCmdFor(Object optStore) Parses command line arguments until the first command argument and set their option values to the option store which is passed as an argument.parseUntilSubCmdWith(OptCfg[] optCfgs) Parses command line arguments with option configurations but stops parsing when encountering first command argument.voidParses command line arguments with option configurations.toString()Returns aStringobject representing the content of this object.
-
Constructor Details
-
Cmd
Constructs an instance of this class with command line arguments.- Parameters:
name- The command name.osArgs- The command line arguments.
-
-
Method Details
-
name
Returns the command name.This name is the base name extracted from the command path string, which is the first element of the command line arguments.
- Returns:
- The command name.
-
args
Returns the command arguments.- Returns:
- The command arguments.
-
hasOpt
Checks whether an option with the specified name exists.- Parameters:
name- The option name.- Returns:
- True, if the option exists.
-
optArg
Returns the option argument with the specified name.If the option has multiple arguments, this method returns the first argument. If the option is a boolean flag, this method returns empty of
Optional. If the option is not specified in the command line arguments, the return value of this method is empty ofOptional.- Parameters:
name- The option name.- Returns:
- An
Optionalobject which may contain the first option argument.
-
optArgs
Returns the option arguments with the specified name.If the option has one or multiple arguments, this method returns an array of the arguments. If the option is a boolean flag, this method returns an
Optionalincluding an empty list. If the option is not specified in the command line arguments, the return value of this method is empty ofOptional.- Parameters:
name- The option name.- Returns:
- An
Optionalobject which may contain the option arguments.
-
optCfgs
Returns the option configurations which was used to parse command line arguments.- Returns:
- The option configurations.
-
toString
Returns aStringobject representing the content of this object. -
parse
Parses command line arguments without configurations.This method divides command line arguments into options and command arguments based on simple rules that are almost the same as POSIX & GNU: arguments staring with
-or--are treated as options, and others are treated as command arguments.If an
=is found within an option, the part before the=is treated as the option name, and the part after the=is treated as the option argument. Options starting with--are long options and option starting with-are short options. Multiple short options can be concatenated into a single command line argument. If an argument is exactly--, all subsequent arguments are treated as command arguments.The results of parsing are stored into this
Cmdinstance. If it is failed to parsing, aInvalidOptionexception is thrown.- Throws:
InvalidOption- If failed to parsing command line arguments.
-
parseUntilSubCmd
Parses command line arguments without configurations but stops parsing when encountering first command argument. This method creates and returns a newCmdinstance that holds the command line arguments starting from the first command argument. This method parses command line arguments in the same way as theparse()method, except that it only parses the command line arguments before the first command argument.- Returns:
- A
Cmdinstance which holds commnd line arguments after a sub command (optional). - Throws:
InvalidOption- If failed to parsing command line arguments.
-
parseWith
Parses command line arguments with option configurations. This method divides command line arguments to command arguments and options. And an option consists of a name and an option argument. Options are divided to long format options and short format options. About long/short format options, since they are same withparsemethod, see the comment of that method. This method allows only options declared in option configurations, basically. An option configuration has fields:storeKey,names,hasArg,isArray,defaults,desc,argInHelp, andvalidator. When an option matches one of thenamesin the option configurations, the option is registered intoCmdwithstoreKey. If bothhasArgandisArrayare true, the option can have one or multiple option arguments, and ifhasArgis true andisArrayis false, the option can have only one option argument, otherwise the option cannot have option arguments. Ifdefaultsfield is specified and no option value is given in command line arguments, the value ofdefaultsis set as the option arguments. If options not declared in option configurations are given in command line arguments, this method basicaly throwsUnconfiguredOptionexception. However, if you want to allow other ooptions, add an option configuration of whichstoreKeyor the first element ofnamesis"*". The option configurations used to parsing are set into theCmdinstance, and it can be retrieved with its method:optCfgs().- Parameters:
optCfgs- The array of the option configurations.- Throws:
InvalidOption- If failed to parsing command line arguments.
-
parseUntilSubCmdWith
Parses command line arguments with option configurations but stops parsing when encountering first command argument. This method creates and returns a newCmdinstance that holds the command line arguments starting from the first command argument. This method parses command line arguments in the same way as theparseWith(com.github.sttk.cliargs.OptCfg[])method, except that it only parses the command line arguments before the first command argument. The option configurations used to parsing are set into theCmdinstance, and it can be retrieved with its method:optCfgs().- Parameters:
optCfgs- The array of the option configurations.- Returns:
- A
Cmdinstance which holds commnd line arguments after a sub command (optional). - Throws:
InvalidOption- If failed to parsing command line arguments.
-
parseFor
Parses command line arguments and set their option values to the fields of the option store which is passed as the argument of this method.This method divides command line arguments to command arguments and options, then sets each option value to a corresponding field of the option store.
Within this method, an array of
OptCfgis made from the fields of the option store. ThisOptCfgarray is set into thisCmdinstance. If you want to access this option configurations, get them byoptCfgs()method.An option configuration corresponding to each field of an option store is determined by its type and
Optfield annotation. If the type is bool, the option takes no argument. If the type is integer, floating point number or string, the option can takes single option argument, therefore it can appear once in command line arguments. If the type is an array, the option can takes multiple option arguments, therefore it can appear multiple times in command line arguments.A
Optfield annotation can have the following pairs of name and value: one iscfgto specifynamesanddefaultsfields ofOptCfgstruct, another isdescto specifydescfield, and yet another isargto specifyargInHelpfield.The format of
cfgis likecfg="f,foo=123". the left side of the equal sign is the option name(s), and the right side is the default value(s). If there is no equal sign, it is determined that only the option name is specified. If you want to specify multiple option names, separate them with commas. If you want to specify multiple default values, separate them with commas and round them with square brackets, like[1,2,3]. If you want to use your favorite character as a separator, you can use it by putting it on the left side of the open square bracket, like/[1/2/3].NOTE: A default value of empty string array option in a field attribute is
[], like@Opt(cfg="=[]"), but it doesn't represent an array which contains only one empty string. If you want to specify an array which contains only one empty string, write nothing after=symbol, likeOpt(cfg="=").- Parameters:
optStore- An object store.- Throws:
InvalidOption- If failed to parsing command line arguments.FailToSetOptionStoreField- If failed to set a field value of an object store.
-
parseUntilSubCmdFor
public Optional<Cmd> parseUntilSubCmdFor(Object optStore) throws InvalidOption, FailToSetOptionStoreField Parses command line arguments until the first command argument and set their option values to the option store which is passed as an argument. This method creates and returns a newCmdinstance that holds the command line arguments starting from the first command argument. This method parses command line arguments in the same way as theparseFor(java.lang.Object)method, except that it only parses the command line argments before the first command argument.- Parameters:
optStore- An object store.- Returns:
- A
Cmdinstance which holds commnd line arguments after a sub command (optional). - Throws:
InvalidOption- If failed to parsing command line arguments.FailToSetOptionStoreField- If failed to set a field value of an object store.
-