6argopt Module
6.1Overview
The argopt
module provides measure to parse option strings in an argument list given through the command line.
Below is an example:
import(argopt)
argopt.Parser {|p|
p.addParam('text', 't')
p.addFlag('test')
p.addFlag('bold', 'b')
try {
[cfg, argv] = p.parse(sys.argv)
} catch {|e|
println(e.text)
sys.exit(1)
}
}
// The value of cfg['text'] is 'foo' when '--text=foo' is specified.
// The value of cfg['test'] is true when '--test' is specified.
6.2argopt.Parser Class
6.2.1Constructor
argopt.Parser() {block?}
argopt.Parser
instance.
6.2.2Method
argopt.Parser#parse(argv[]:string)
Parses an argument list which is usually the value of sys.argv
given by sys
module.
It returns the result in a format [cfg, argv]
where cfg
is a dict
instance containing parameter values and argv
a list of arguments that have not been parsed as options.
argopt.Parser#addParam(longName:string, shortName?:string, help?:string, helpValue?:string, defValue?:string)
Adds an option that comes with a value like --foo=value
where foo
is a long name for the option.
The argument longName
specifies a long option name that follows after two hyphens in the command line. This name is also used as a key when you look for a value in the dictionary cfg
returned by argopt.Parser#parse()
.
The argument shortName
specifies a short option name that usually consists of a single character. If it exists, you can specify the option by a character followed by one hyphen like -f value
where f
is the short name.
The argument help
and helpValue
are used in a option parameter help created by argopt.Parse#formatHelp()
. The string for help
specifies a help text for the option while helpValue
is a string printed after the equal character in the option presentation. If the argument helpValue
is not specified, a string X
is printed instead.
The argument defValue
specifies a default value that would be used when the option is not specified in the command line.
argopt.Parser#addFlag(longName:string, shortName?:string, help?:string)
Adds an option that represents a boolean state. It comes like --foo
where foo
is a long name for the option.
The argument longName
specifies a long option name that follows after two hyphens in the command line. This name is also used as a key when you look for a value in the dictionary cfg
returned by argopt.Parser#parse()
.
The argument shortName
specifies a short option name that usually consists of a single character. If it exists, you can specify the option by a character followed by one hyphen like -f
where f
is the short name.
The argument help
is used in a option parameter help created by argopt.Parse#formatHelp()
. The string for help
specifies a help text for the option.
argopt.Parser#formatHelp(longNameFlag:boolean => true, shortNameFlag:boolean => true):[linefeed]
Creates an iterator of strings that contain help text for each option.
If the argument longNameFlag
is true
, the help text would contain long names.
If the argument shortNameFlag
is true
, the help text would contain short names.
In default, each string doesn't contain a line feed at the end. To add a line feed, specify an attribute :linefeed
.
Below is an example of showing help:
argopt.Parser {|p|
p.addParam('text', 't', 'text value', 'txt')
p.addFlag('flag1', 'f', 'flag option #1')
p.addFlag('flag2', 'g', 'flag option #2')
println(p.formatHelp())
}
The result comes as below:
-t, --text=txt text value
-f, --flag1 flag option #1
-g, --flag2 flag option #2