Class

class Argopt

public class ArgOpt {
    public init(shortArgFormat: String)
    public init(longArgList: Array<String>)
    public init(shortArgFormat: String, longArgList: Array<String>)
    public init(args: Array<String>, shortArgFormat: String, longArgList: Array<String>)
}

Description: Argopt is used to parse command line parameters and obtain parsing results.

A command line parameter consists of a prefix symbol, a parameter name, and a parameter value.

"-" indicates the prefix of a short parameter (short command line parameter), and "--" indicates the prefix of a long parameter (long command line parameter). The name of a short parameter that can be parsed can only be a letter. The name of a long parameter that can be parsed must start with a letter and cannot contain "=".

For example, in command parameters "-a123" and "--target=abc.txt", "a" is the short parameter name of -a123, and "target" is the long parameter name of --target=abc.txt. "-123" and "--tar=get=abc.txt" are incorrect command line parameters.

This class allows users to specify parameter names and parameter strings, and provides methods for parsing strings based on parameter names.

The formats of short and long parameter names are as follows:

  • Format of a short parameter name string parameter: a letter + ":"; correct example: "a:"; special example: "ab:". In the special example, only "b" is used as the short parameter name through parsing.
  • Format of the parameter of a long parameter name string array: "--" (optional) + long parameter name + "="; example: "--testA=" or "testA=".

When a command line parameter is parsed based on the parameter name, if the format of the command line parameter is correct and the corresponding parameter name exists, the command line parameter can be correctly parsed and the result can be obtained by users. Otherwise, the command line parameter cannot be parsed.

For example, if the parameter name is "a:b:" and command line parameters are "-a123 -cofo", the command line parameter whose parameter name is "a" and parameter value is "123" is parsed, and "-cofo" is not parsed.

init(Array<String>)

public init(longArgList: Array<String>)

Description: Constructs an ArgOpt instance and parses the long parameter name from the string of the list.

Parameters:

  • longArgList: Array<String>: string array containing the long parameter name

Throws:

  • IllegalArgumentException: If the long parameter name string in the string array does not comply with the specifications, the string does not comply with the UTF-8 encoding, or a character does not have the corresponding Unicode character, this exception is thrown.

init(Array<String>, String, Array<String>)

public init(args: Array<String>, shortArgFormat: String, longArgList: Array<String>)

Description: Constructs an ArgOpt instance, parses the short parameter name from the short parameter name string, and parses the long parameter name from the string of the list. If the parsing is successful, parses the value of the parameter name from the command line parameter specified by the args parameter based on the parameter name obtained through parsing.

Parameters:

  • args: Array<String>: command line parameter string array to be parsed
  • shortArgFormat: String: string containing the short parameter name
  • longArgList: Array<String>: string array containing the long parameter name

Throws:

  • IllegalArgumentException: If the short parameter name string does not comply with the specifications, the long parameter name string in the string array does not comply with the specifications, the string does not comply with the UTF-8 encoding, or a character does not have the corresponding Unicode character, this exception is thrown.

init(String)

public init(shortArgFormat: String)

Description: Constructs an ArgOpt instance and parses the short parameter name from the short parameter name string.

Parameters:

  • shortArgFormat: String: string containing the short parameter name

Throws:

  • IllegalArgumentException: If the short parameter name string does not comply with the specifications, the string does not comply with the UTF-8 encoding, or a character does not have the corresponding Unicode character, this exception is thrown.

init(String, Array<String>)

public init(shortArgFormat: String, longArgList: Array<String>)

Description: Constructs an ArgOpt instance, parses the short parameter name from the short parameter name string, and parses the long parameter name from the string of the list.

Parameters:

  • shortArgFormat: String: string containing the short parameter name
  • longArgList: Array<String>: string array containing the long parameter name

Throws:

  • IllegalArgumentException: If the short parameter name string does not comply with the specifications, the long parameter name string in the string array does not comply with the specifications, the string does not comply with the UTF-8 encoding, or a character does not have the corresponding Unicode character, this exception is thrown.

func getArg(String)

public func getArg(arg: String): Option<String>

Description: Returns the parsed value of the parameter specified by arg.

Parameters:

  • arg: String: string consisting of the prefix and parameter name (the prefix can be omitted)

Returns:

func getArgumentsMap()

public func getArgumentsMap(): HashMap<String, String>

Description: Obtains all parsed parameter names and values and returns them in a hash table.

Returns:

  • HashMap < String, String >: hash table in which parsed parameter names are keys and parameter values are values

func getUnparseArgs()

public func getUnparseArgs(): Array<String>

Description: Returns command line parameters that are not parsed.

Returns:

  • Array<String>: array storing strings that are not parsed