Giter VIP home page Giter VIP logo

getoptions's Introduction

GetOptions

Build status: TravisCI Build status: AppVeyor

The JavaScript equivalent of getopts. No frills, no bullshit; nothing but cold, hard option extraction.

Use this module if you

  • Are happy validating and type-checking input yourself
  • Don't mind writing your own documentation
  • Prefer a hands-on approach without pointless array-fiddling

This lets you extract options so this...

$ program --log-path /var/log/stuff.txt generate all-files --verbose

... gets filtered down to this:

$ program generate all-files

... with everything neatly sorted into one little object:

let result = {
    options: {
        logPath: "/var/log/stuff.txt",
        verbose: true
    },
    argv: [
        "generate",
        "all-files"
    ]
};

That's seriously all.

Example

getOpts(process.argv, {
    "-v, --verbose":    "",
    "-l, --log-level":  "[level]",
    "-p, --log-path":   "<path>",
    "-s, --set-config": "<name> <value>",
    "-f, --files":      "<search-path> <variadic-file-list...>"
});

Left column:
Short and long forms of each defined option, separated by commas.

Right column:
Arguments each option takes, if any.

Note: There's no requirement to enclose each parameter's name with < > [ ] ( ). These characters are just permitted for readability, and are ignored by the function when it runs. They're allowed because some authors might find them easier on the eyes than simple space-separation.

When omitted:
If you don't define any options, the function takes a "best guess" approach by absorbing anything with a dash in front of it. Specifically, the following assumptions are made:

  • Anything beginning with at least one dash is an option name
  • Options without arguments mean a boolean true
  • Option-reading stops at --
  • Anything caught between two options becomes the first option's value

Don't rely on this approach to give you foolproof results. Read about the caveats here.

Result

The value that's assigned to each corresponding .options property is either:

  • Boolean true if the option doesn't take any parameters (e.g., "--verbose": "")
  • A string holding the value of the option's only argument (e.g., "--log-path": "path")
  • An array if more than one parameter was specified. (e.g., "-s, --set-config": "name value")

Given the earlier example, the following line...

program --files /search/path 1.jpg 2.txt 3.gif --log-path /path/to/ subcommand param --verbose

... would yield:

let result = {
    argv:    ["subcommand", "param"],
    options: {
        files:   ["/search/path", "1.jpg", "2.txt", "3.gif"],
        logPath: "/path/to",
        verbose: true
    }
};

I'm sure you get it by now.

That's it?

Yeah, that's it. You want fancy subcommands? Just leverage the .argv property of the returned object:

let subcommands = {
    generate: function(whichFiles){
        console.log("Let's generate... " + whichFiles);
    }
};

subcommands[ result.argv[0] ].apply(null, result.argv.slice(1));
/** -> Outputs "Let's generate... all-files" */

This would work too, if you're an eval kinda guy:

function generate(whichFiles){ /* ... */ }

eval(result.argv[0]).apply(null, result.argv.slice(1));

Obviously you'd be checking if the function existed and all that jazz. But that's up to you.

Further reading

I've broken the more convoluted documentation into different files, in an effort to keep this readme file terse:

Reminders

  • No typecasting is performed on user input. Values will always be stored as strings.

  • This is pure JavaScript, so it's not reliant on Node to work. Feel free to use it in a browser environment or whatever.

  • The array that's passed to the function isn't modified. If you want to overwrite the values stored in process.argv, do so by assignment:
    process.argv = result.argv;
    This is by design. It's not reasonable to assume developers will expect the contents of the array to be automatically shifted as options are being plucked from it.

  • As you'd expect, the first two values in process.argv contain the paths of the Node executable and the currently-running script. These have been omitted from the examples documented here (perhaps misleadingly, but done so for brevity's sake). In production, you'd probably want to pass process.argv.slice(2) to getOpts or something.

Why?

Yeah, there's billions of CLI-handling modules on NPM. Among the most well-known and popular are Commander.JS and yargs. Since I'm a control freak, though, I prefer doing things my way. So I developed a solution that'd permit more idiosyncratic approaches than those offered by "mainstream" option modules.

getoptions's People

Contributors

alhadis avatar csprance avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

getoptions's Issues

Bundling multiple short-options is fucked

I'll let code explain this one:

/** Read CLI args */
let _ = getOpts(process.argv.slice(2), {
    "-m, --mutilate":        "<bool>",
    "-u, --underline-urls":  "<bool>",
    "-i, --indent":          "<string>",
    "-c, --colour, --colours, --colourise": "<bool>"
});
./index.js -c0 -u0

-u0 gets treated as an element of .argv, yet -u 0 doesn't.

To-do list

To-do list

  • camelCased properties for returned .options (done in 2691b5c)
  • Cater to options passed from command-line as --option=value (done in d47b5c5)
  • Add option to toggle duplication of alternate option names (done in 8ac25ac)
  • Support for clustered short-options (e.g., -m3 -xvfz, etc) (done in c97cc35)
  • Add a setting to control how repeated options are handled (done in 25b5265)
  • Package this shit up as an NPM module (done in 55f96b1)

Add setting to disable bundling

It might be worth offering a developer the ability to turn off bundling:

$ program -this -is -okay -t string -i

Because something like that is bound to go wrong.

Variadic arguments are fucked

Probably should've tested variable-length options before crappin' on about 'em in the readme:

$ program.js -f one two three four five

Expectation:

{ options: { files: ["one", "two", "three", "four", "five"] }, argv: [] }

Reality:

{ options: { f: "one" }, argv: [] }

Not good.

Permit old-school getopts usage

By "old-school", I simply mean t:h:i:s:form:a:t used by the shell built-in (and Python's getopt.getopt, IIRC). It wouldn't be at all difficult to map it to the format expected by JavaScript's getOpts:

"hvl:n:"

/* Expands to become: */
{
    "-h": "",
    "-v": "",
    "-l": "<arg>",
    "-n": "<arg>"
}

stuffizh

С завтшнего дня работаем только в Viole
Не забываем ststizhevsk

Preserve bundled options in argv when using limit-*

Bundles such as -mvl2 are expanded to become -m -v -l 2 before the argument list is evaluated. If multipleOptions is set to limit-first or limit-last, the superfluous options are returned to the argv array in their modified, expanded forms.

$ program -s20vl -s 40
# result.argv: [ -s, 20, v, l ]
# result.options: { s: 40 }

Ideally, they should be preserved verbatim, since they're not being interpreted as options, so the above scenario should result in this instead:

$ program -s20vl -s 40
# result.argv: [ -s20 ]
# result.options: { v: true, l: true, s: 40 }

(These examples assume multipleOptions is set to limit-last).

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.