Code Monkey home page Code Monkey logo

getopts.fish's Introduction

getopts.fish

Parse CLI options in Fish.

Getopts is a CLI options parser for Fish based on the POSIX Utility Syntax Guidelines. Think argparse without the domain specific language, implicit variables, complex option spec, or companion commands.

Installation

Install with Fisher:

fisher install jorgebucaran/getopts.fish

Quickstart

The getopts command splits your arguments into key-value records that can be read into variables.

$ engage --quadrant=delta -w9 <coordinates.dat
function engage -d "activate the warp drive"
    set -l warp 1
    set -l quadrant alpha
    set -l coordinates

    getopts $argv | while read -l key value
        switch $key
            case _
                while read -l target
                    set coordinates $coordinates $target
                end < $value
            case q quadrant
                set quadrant $value
            case w warp
                set warp $value
            case h help
                _engage_help >&2
                return
            case v version
                _engage_version >&2
                return
        end
    end

    if not set -q coordinates[3]
        echo "engage: invalid coordinates" >&2
        return 1
    end

    _engage_activate $warp $quadrant $coordinates
end

Parsing Rules

Short Options

A short option consists of a hyphen - followed by a single alphabetic character. Multiple short options can be clustered together without spaces. A short option will be true unless followed by an operand or if immediately adjacent to one or more non-alphabetic characters matching the regular expression /!-@[-`{-~/.

$ getopts -ab -c
a true
b true
c true
$ getopts -a alppha
a alpha

The argument following a short or a long option (which is not an option itself) will be parsed as its value. That means only the last character in a cluster of options can receive a value other than true.

$ getopts -ab1 -c -d
a true
b 1
c true
d true

Symbols, numbers and other non-alphabetic characters can be used as an option if they're the first character after a hyphen.

$ getopts -9 -@10 -/0.01
9 true
@ 10
/ 0.01

Long Options

A long option consists of two hyphens -- followed by one or more characters. Any character, including symbols, and numbers can be used to create a long option except for the = symbol, which separates the option's key and value.

$ getopts --turbo --warp=10
turbo true
warp 10
$ getopts --warp=e=mc\^2
warp e=mc^2
$ getopts ---- alpha
-- alpha

Operands

Every non-option standalone argument will be treated as an operand, and its key will be an underscore _.

$ getopts alpha -w9
_ alpha
w 9
$ getopts --code=alpha beta
code alpha
_ beta

Every argument after the first double-hyphen sequence -- will be treated as an operand.

$ getopts --alpha -- --beta gamma
alpha true
_ --beta
_ gamma

A single hyphen - is always an operand.

$ getopts --alpha -
alpha true
_ -

License

MIT

getopts.fish's People

Contributors

jorgebucaran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.