• Stars
    star
    214
  • Rank 178,289 (Top 4 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parse CLI options in Fish

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

More Repositories

1

hyperapp

1kB-ish JavaScript framework for building hypertext applications
JavaScript
19,021
star
2

fisher

A plugin manager for Fish
Shell
7,219
star
3

awsm.fish

A curation of prompts, plugins & other Fish treasures ๐Ÿš๐Ÿ’Ž
3,819
star
4

cookbook.fish

From Shell to Plate: Savor the Zest of Fish ๐Ÿฆž
1,933
star
5

nvm.fish

The Node.js version manager you'll adore, crafted just for Fish
Shell
1,890
star
6

colorette

๐ŸŒˆEasily set your terminal text color & styles
JavaScript
1,573
star
7

superfine

Absolutely minimal view layer for building web interfaces
JavaScript
1,562
star
8

classcat

Build a class attribute string quickly
JavaScript
901
star
9

getopts

Node.js CLI options parser
JavaScript
634
star
10

hydro

Ultra-pure, lag-free prompt with async Git statusโ€”just for Fish
Shell
559
star
11

hyperawesome

A curated list of awesome projects built with Hyperapp + more
490
star
12

twist

Declarative JavaScript Testing
JavaScript
378
star
13

replay.fish

Run Bash commands, replay changes in Fish ๐Ÿค
Shell
370
star
14

fishtape

100% pure-Fish test runner
Shell
340
star
15

spark.fish

โ–โ–‚โ–„โ–†โ–‡โ–ˆโ–‡โ–†โ–„โ–‚โ–
Shell
322
star
16

autopair.fish

Auto-complete matching pairs in the Fish command line
Shell
316
star
17

hyperapp-router

Declarative routing for Hyperapp V1 using the History API.
JavaScript
257
star
18

gitio.fish

Create a custom git.io URL
Shell
88
star
19

hyperapp-html

Html helper functions for Hyperapp V1
JavaScript
81
star
20

pyenv

Pyenv support plugin for fish-shell
Shell
62
star
21

humantime.fish

Turn milliseconds into a human-readable string in Fish
Shell
20
star
22

.github

My health files
1
star
23

jorgebucaran.github.io

HTML
1
star