Skip to content

puruneko/cmdlnParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Caution
Under construction

cmdlnParser

cmdlnParser is a tool to make command-line-parameter processing easily for Nim!

Features

The concept of this tool is to simplify bothersome command-line-parameter setting, checking and processing.

1. simple settings

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 and separator 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 …​)

        Warning
        in 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…​

2. wrap troublesome work

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.

Sample

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.

step1

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

step2

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)
)

step3

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).

step4

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)

step5

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).

overall code of this sample

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)

Special required library

Not applicable

TODO

  • ❏ improve type estimation

  • ✓ corresponds to Boolean type

About

[Nim] command line processing tool

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages