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.boolean
Checks 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.void
parse()
Parses command line arguments without configurations.void
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.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.void
Parses command line arguments with option configurations.toString()
Returns aString
object 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
Optional
object 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
Optional
including 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
Optional
object 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 aString
object 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
Cmd
instance. If it is failed to parsing, aInvalidOption
exception 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 newCmd
instance 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
Cmd
instance 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 withparse
method, 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 thenames
in the option configurations, the option is registered intoCmd
withstoreKey
. If bothhasArg
andisArray
are true, the option can have one or multiple option arguments, and ifhasArg
is true andisArray
is false, the option can have only one option argument, otherwise the option cannot have option arguments. Ifdefaults
field is specified and no option value is given in command line arguments, the value ofdefaults
is set as the option arguments. If options not declared in option configurations are given in command line arguments, this method basicaly throwsUnconfiguredOption
exception. However, if you want to allow other ooptions, add an option configuration of whichstoreKey
or the first element ofnames
is"*"
. The option configurations used to parsing are set into theCmd
instance, 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 newCmd
instance 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 theCmd
instance, and it can be retrieved with its method:optCfgs()
.- Parameters:
optCfgs
- The array of the option configurations.- Returns:
- A
Cmd
instance 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
OptCfg
is made from the fields of the option store. ThisOptCfg
array is set into thisCmd
instance. 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
Opt
field 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
Opt
field annotation can have the following pairs of name and value: one iscfg
to specifynames
anddefaults
fields ofOptCfg
struct, another isdesc
to specifydesc
field, and yet another isarg
to specifyargInHelp
field.The format of
cfg
is 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 newCmd
instance 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
Cmd
instance 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.
-