Caution
|
Under construction |
cmdlnParser is a tool to make command-line-parameter processing easily for Nim!
The concept of this tool is to simplify bothersome command-line-parameter setting, checking and processing.
By creating parameter setting object, you can complete to define what parameters this program has and how to process each parameter.
-
flexible parameter settings
-
You should just code default value or processing procedure.
-
You can create original parameter format.
-
This tool supports
prefix
andseparator
format according to "{prefix}{keyword}{separator}{input}
. -
ex:
--path:input
" ,/p input
etc.
-
-
-
automatic detection of types
-
You do not have to specify any generics type like
…[type](…)
. -
The above explain contains original types (but must code procedure of converting).
-
This tool judges parameter type as string if you set only parameter keyword.
-
-
the setting function attentive to needs
-
This tool supports various parameter patterns.
-
no prefix parameter (like
name.exe param1 param2 …
)Warningin this case, type checking is done in the order in which they are set. -
no separator parameter (like
-n50
) -
default value
-
You can select parameters that have default value or not
-
-
multiple parameter keyword
-
You can let different parameter keywords have the same meaning.
-
-
-
Each parameter has own description.
-
You can see the help documentation by inputting parameter like
-h
.
-
-
On going…
To process command-line-parameters, you just call parse
procedure with setting object.
-
This procedure wraps various checking.
-
check number of parameters
-
check type of parameters
-
-
This procedure returns arranged tuple.
-
By linking parameter keyword and return value (by settings), this tool returns arranged tuple.
-
It means that the returned values do not set in the order they are located in calling procedure, but set in order by link settings.
-
-
If you let your program process with some command-line-parameters, try this tool with simple steps.
In this sample, it is considered that you will create a program to search the target directory.
Design parameters you need such as example below.
- --path:
-
This option defines target path.
setting name value type
string
parse proc
nothing
default
nothing
specific options
nothing
- --search:
-
This option defines search strings.
This option will be set any number.setting name value type
string
parse proc
nothing
default
nothing
specific options
multiple parameters permitted
- --keyword:
-
This option is an alias of search option.
setting name value type
string
parse proc
nothing
default
nothing
specific options
multiple parameters permitted
- --regex:
-
This option defines to use regex or not.
This option must be defined only once.setting name value type
bool
parse proc
nothing
default
false
specific options
multiple parameters banned
Commit your design by creating object of CmdlnParam
object using newCmdlnParam
procedure.
Caution
|
object must be defined as var definition.
|
var cmdlnParser = newCmdlnParser(
newCmdlnParam("path", options=BAN_MULTIPLE),
newCmdlnParam("search"),
newCmdlnParam("keyword"),
newCmdlnParam("regex", false, options=BAN_MULTIPLE)
)
Define relationship between options and result values.
In this sample, search
option and keyword
option should be returned as the same result value.
Also, the order of result values can be defined at the same time.
order | keyword |
---|---|
first |
path |
second |
search, keyword |
third |
regex |
Then, update the object created in step 2 in order to add the results
option, as below:
var cmdlnParser = newCmdlnParser(
results=("path",["search","keyword"],"regex"),
newCmdlnParam("path", options=BAN_MULTIPLE),
newCmdlnParam("search"),
newCmdlnParam("keyword"),
newCmdlnParam("regex", false, options=BAN_MULTIPLE)
)
Note
|
prefix and separator can be also defined at this newCmdlnParser procedure (pre option and sep option).
|
Call parse
procedure which CmdlnParser
object has (as correctly, set it as the first argument).
command-line-parameters you want to parse are second argument.
let cmdlineParams = os.getCommandlineParams()
let res = cmdParser.parse(cmdlineParams)
If you come here, the parsing result shall be assigned to the res
variable.
For example, if command-line-params are
["--path:/var","--search:test","--keyword:test2","--regex:true"]
,
you can see
("/var", @["test","test2"], true)
.
import os
import cmdlnparser
var cmdlnParser = newCmdlnParser(
results=("path",["search","keyword"],"regex"),
newCmdlnParam("path", options=BAN_MULTIPLE),
newCmdlnParam("search"),
newCmdlnParam("keyword"),
newCmdlnParam("regex", false, options=BAN_MULTIPLE)
)
let cmdlineParams = os.getCommandlineParams()
let res = cmdlnParser.parse(cmdlineParams)