Git Product home page Git Product logo

nim-simpleargs's Introduction

Simpleargs package

A simple package for parsing command line arguments with basic functionality. This is intended for quick arg parsing with minimum dependencies, for more complex scenarios, see alternatives like the argparse package.

Quick start

The essential usage is to create a parsing schema and then parse command line arguments

var p: ParseSchema
    p.initParser("my tool"):
        p.addOption("-i", "--input", help="path to input", required=true, default="input.txt"):
        p.addOption("-o", "--out", help="Output file")        
        p.addFlag(long="--flag_on", help="This flag is true")

    var opts = p.parseOptions()

The help message is automatically print if --help or -h are parsed from command-line.

Retrieve values

  • An argument value can be retrieved using [] notation on the object from parseOptions(), like opts["input"]. In this case a string is returned. The same works for positional arguments using an int like opts[0].

  • You can also automatically get the correct var type using the getOpt method on a previously declared var, like

    var x: int
    opts.getOpt("input", x)

    This will raise and exception if the value from the argument cannot be casted to the requested data type.

  • You can use isSet method to check if an argument was set at the command line, like opts.isSet("input") return true if --input was used at the command line. Note that at the moment this check strictly so the above example returns false if the short version of the option -i was used instead.

Limitations

  1. At the moment when only long option name is specified this needs to be explicit. So this works addOption(long="--input", help="help text"), but this does not work addOption("--input", help="help text").

  2. The return object after parsing will contain both short and long option names if both were set.

  3. No support for option with multiple values, if an option is seen multiple times during parsing this will result in an error.

  4. When option are set at command line using the short name, the value need to be specified with =. So this works mytool -i=input.txt, but this does not work mytool -i input.txt.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.