2.3.32 • Published 9 days ago

@thi.ng/args v2.3.32

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 days ago

args

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Declarative, functional & typechecked CLI argument/options parser, value coercions etc..

Includes built-in support for the following argument types (of course custom arg types are supported too):

Argument typeMultipleExampleResult
Flag--force, -fforce: true
String--foo barfoo: "bar"
Float/int/hex--bg ff997fbg: 16750975
Enum--type pngtype: "png"
KV pairs--define foo=bardefine: { foo: "bar" }
KV multi pairs-D foo=bar -D foo=bazdefine: { foo: ["bar", "baz"] }
JSON--config '{"foo": [23]}'config: { foo: [23] }
Fixed size tuple--size 640x480size: { value: [640, 480] }

If multiple values/repetitions are allowed for an argument, the values will be collected into an array (apart from KV pairs, which will yield an object). Furthermore, for multi-args, an optional delimiter can be specified to extract individual values, e.g. -a 1,2,3 equals -a 1 -a 2 -a 3

Status

STABLE - used in production

Search or submit any issues for this package

Installation

yarn add @thi.ng/args

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/args"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const args = await import("@thi.ng/args");

Package sizes (gzipped, pre-treeshake): ESM: 2.44 KB

Dependencies

API

Generated API docs

Basic usage

type ImgType = "png" | "jpg" | "gif" | "tiff";

// CLI args will be validated against this interface
interface TestArgs {
    configPath?: string;
    force: boolean;
    bg: number;
    type: ImgType;
    size?: Tuple<number>;
    pos?: Tuple<number>;
    xtra?: { a: number; b: string };
    define?: KVDict;
}

// arg specifications
const specs: Args<TestArgs> = {
    // string arg
    configPath: string({
        alias: "c",
        hint: "PATH",
        desc: "Config file path (CLI args always take precedence over those settings)",
    }),
    // boolean flag (default: false)
    force: flag({
        alias: "f",
        desc: "Force operation",
        // side effect & predicate
        // parsing only continues if fn returns true
        fn: (_) => (console.log("force mode enabled"), true),
    }),
    // hex int value
    bg: hex({
        desc: "Background color",
        // mandatory args require a `default` value and/or `optional: false`
        default: 0xffffff,
    }),
    // enum value (mandatory)
    type: oneOf(["png", "jpg", "gif", "tiff"], {
        alias: "t",
        desc: "Image type",
        // mandatory args require a `default` value and/or `optional: false`
        optional: false,
    }),
    // fixed size numeric tuple w/ `x` as delimiter
    // size: tuple(coerceInt, 2, { hint: "WxH", desc: "Target size" }, "x"),
    // syntax sugar for above:
    size: size(2, { hint: "WxH", desc: "Target size" }),
    // another version for tuples of floating point values
    // pos: tuple(coerceFloat, 2, { desc: "Lat/Lon" }, ","),
    pos: vec(2, { desc: "Lat/Lon" }),
    // JSON string arg
    xtra: json({
        alias: "x",
        desc: "Extra options",
    }),
    // key-value pairs parsed into an object
    define: kvPairs({ alias: "D", desc: "Define dict entry" }),
};

try {
    // parse argv w/ above argument specs & default options
    // (by default usage is shown if error occurs)
    const args = parse(specs, process.argv);
    console.log(args);
} catch (_) {}

Generate & display help

Usage information can be generated via usage() and is automatically triggered via the special --help option (configurable, see ParseOpts).

Each arg can be associated with arbitrary group IDs, which are then used to segment usage output. By default, flag() args are assigned to a "flags" group, all others to "main". The default output order too is ["flags", "main"], but can be configured via a group option given an arg spec or factory function.

By default, ANSI colors are used to format the result string of usage(), but can be disabled (see UsageOpts).

ts-node index.ts --help

-f, --force                     Force operation

--bg HEX                        Background color
-c PATH, --config-path PATH     Config file path (CLI args always take
                                precedence over those settings)
-D key=val, --define key=val    [multiple] Define dict entry
--pos N,N                       Lat/Lon
--size WxH                      Target size
-t ID, --type ID                [required] Image type: 'png', 'jpg', 'gif',
                                'tiff'
-x JSON, --xtra JSON            Extra options

Parsing, value coercions & side effects

The below invocation demonstrates how the various argument types are handled & represented in the result. Parsing stops with the first non-argument value (here sourcefile.png) and the remaining values are made available via rest in the result object.

ts-node index.ts \
    -f -t png --bg ff00ff --size 640x480 \
    -D author=toxi -D date=2018-03-24 \
    --xtra '{"foo": [23]}' \
    sourcefile.png

# force mode enabled
# {
#   result: {
#     force: true,
#     type: 'png',
#     bg: 16711935,
#     size: Tuple { value: [640, 480] }
#     define: { author: 'toxi', date: '2018-03-24' },
#     xtra: { foo: [23] },
#   },
#   index: 15,
#   rest: [ 'sourcefile.png' ],
#   done: false
# }

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-args,
  title = "@thi.ng/args",
  author = "Karsten Schmidt",
  note = "https://thi.ng/args",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

2.3.32

9 days ago

2.3.31

12 days ago

2.3.30

21 days ago

2.3.29

24 days ago

2.3.28

1 month ago

2.3.27

1 month ago

2.3.26

1 month ago

2.3.25

2 months ago

2.3.24

2 months ago

2.3.23

2 months ago

2.3.22

2 months ago

2.3.21

2 months ago

2.3.20

2 months ago

2.3.19

2 months ago

2.3.18

2 months ago

2.3.17

2 months ago

2.3.16

2 months ago

2.3.15

2 months ago

2.3.14

2 months ago

2.3.13

3 months ago

2.3.12

3 months ago

2.3.11

3 months ago

2.3.8

3 months ago

2.3.7

3 months ago

2.3.9

3 months ago

2.3.10

3 months ago

2.3.6

3 months ago

2.3.5

3 months ago

2.3.4

4 months ago

2.3.3

4 months ago

2.3.2

4 months ago

2.3.0

5 months ago

2.3.1

4 months ago

2.2.46

5 months ago

2.2.45

5 months ago

2.2.44

5 months ago

2.2.28

9 months ago

2.2.29

9 months ago

2.2.39

6 months ago

2.2.37

6 months ago

2.2.38

6 months ago

2.2.35

8 months ago

2.2.36

8 months ago

2.2.33

8 months ago

2.2.34

8 months ago

2.2.31

9 months ago

2.2.32

8 months ago

2.2.30

9 months ago

2.2.42

6 months ago

2.2.43

5 months ago

2.2.40

6 months ago

2.2.27

11 months ago

2.2.26

12 months ago

2.2.25

1 year ago

2.2.24

1 year ago

2.2.22

1 year ago

2.2.23

1 year ago

2.2.20

1 year ago

2.2.21

1 year ago

2.2.17

1 year ago

2.2.19

1 year ago

2.2.15

1 year ago

2.2.16

1 year ago

2.2.13

1 year ago

2.2.14

1 year ago

2.2.12

1 year ago

2.2.3

2 years ago

2.2.5

2 years ago

2.2.4

2 years ago

2.2.11

1 year ago

2.2.7

2 years ago

2.2.6

2 years ago

2.2.10

1 year ago

2.2.9

1 year ago

2.2.8

2 years ago

2.2.2

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.14

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.8

2 years ago

2.1.7

2 years ago

2.1.9

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.0.8

2 years ago

2.1.0

2 years ago

2.0.7

2 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago