• Stars
    star
    114
  • Rank 298,679 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 12 months ago

Reviews

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

Repository Details

⛳️ Typed command-line arguments parser for Node.js

type-flag

Typed command-line arguments parser. Only 1.4 kB.

Try it out online

Looking for something more robust? 👀

Try Cleye—a CLI development tool powered by type-flag.

In addition to flag parsing, it supports argument parsing and has a beautiful --help documentation generator.

Found this package useful? Show your support & appreciation by sponsoring! ❤️

🚀 Install

npm i type-flag

🚦 Quick start

type-flag offers a simple API to parse command-line arguments.

Let's say you want to create a script with the following usage:

$ my-script --name John --age 20

typeFlag

Here's how easy it is with type-flag:

import { typeFlag } from 'type-flag'

const parsed = typeFlag({
    name: String,
    age: {
        type: Number,
        alias: 'a'
    }
})

console.log(parsed.flags.name) // 'John'
console.log(parsed.flags.age) // 20

You can also get unknown flags and arguments from the parsed object:

// object of unknown flags passed in
console.log(parsed.unknownFlags)

// arguments
console.log(parsed._)

getFlag

Want something even simpler?

type-flag also exports a getFlag function that returns a single flag value.

import { getFlag } from 'type-flag'

const name = getFlag('--name', String)
const age = getFlag('-a,--age', Number)

console.log(name) // 'John'
console.log(age) // 20

These are quick demos but type-flag can do so much more:

  • Accept multiple flag values
  • Flag operators (e.g. =) for explicitly passing in a value
  • Parse unknown flags
  • Parse alias groups (e.g. -abc)

Keep reading to learn more!

🧑‍💻 Usage

Defining flags

Pass in an object where the key is the flag name and the value is the flag type—a parser function that takes in a string and parses it to that type. Default JavaScript constructors should be able to cover most use-cases: String, Number, Boolean, etc.

The value can also be an object with the type property as the flag type.

typeFlag({
    // Short-hand
    stringFlag: String,
    numberFlag: Number,
    booleanFlag: Boolean,

    // Object syntax:
    stringFlag: {
        type: String
    }
})

Array type

To accept multiple values of a flag, wrap the type with an array:

const parsed = typeFlag({
    myFlag: [String]
})

// $ node ./cli --my-flag A --my-flag B
parsed.flags.myFlag // => ['A', 'B']

Aliases

Flags are often given single-character aliases for shorthand usage (eg. --help to -h). To give a flag an alias, use the object syntax and set the alias property to a single-character name.

typeFlag({
    myFlag: {
        type: String,
        alias: 'm'
    }
})

// $ node ./cli -m hello
parsed.flags.myFlag // => 'hello'

Default values

Flags that are not passed in will default to being undefined. To set a different default value, use the object syntax and pass in a value as the default property. When a default is provided, the return type will reflect that instead of undefined.

When using mutable values (eg. objects/arrays) as a default, pass in a function that creates it to avoid mutation-related bugs.

const parsed = typeFlag({
    someNumber: {
        type: Number,
        default: 1
    },

    manyNumbers: {
        type: [Number],

        // Use a function to return an object or array
        default: () => [1, 2, 3]
    }
})

To get undefined in the parsed flag type, make sure strict or strictNullChecks is enabled.

kebab-case flags mapped to camelCase

When passing in the flags, they can be in kebab-case and will automatically map to the camelCase equivalent.

const parsed = typeFlag({
    someString: [String]
})

// $ node ./cli --someString hello --some-string world
parsed.flags.someString // => ['hello', 'world']

Unknown flags

When unrecognized flags are passed in, they are interpreted as a boolean, or a string if explicitly passed in. Unknown flags are not converted to camelCase to allow for accurate error handling.

const parsed = typeFlag({})

// $ node ./cli --some-flag --some-flag=1234
parsed.unknownFlags // => { 'some-flag': [true, '1234'] }

Arguments

Arguments are values passed in that are not associated with any flags. All arguments are stored in the _ property.

Everything after -- (end-of-flags) is treated as an argument (including flags) and will be stored in the _['--'] property.

const parsed = typeFlag({
    myFlag: [String]
})

// $ node ./cli --my-flag value arg1 -- --my-flag world
parsed.flags.myFlag // => ['value']
parsed._ // => ['arg1', '--my-flag', 'world']
parsed._['--'] // => ['--my-flag', 'world']

Flag value delimiters

The characters =, : and . are reserved for delimiting the value from the flag.

$ node ./cli --flag=value --flag:value --flag.value

This allows for usage like --flag:key=value or --flag.property=value to be possible.

Mutated argv array

When type-flag iterates over the argv array, it removes the tokens it parses out via mutation.

By default, type-flag works on a new copy of process.argv.slice(2) so this doesn't have any side-effects. But if you want to leverage this behavior to extract certain flags and arguments, you can pass in your own copy of process.argv.slice(2).

This may be useful for filtering out certain flags before passing down the argv to a child process.

Ignoring unknown flags

Sometimes it may be undesirable to parse unknown flags. In these cases, you can ignore them so they're left unparsed in the argv array.

const argv = process.argv.slice(2)
const parsed = typeFlag(
    {},
    argv,
    {
        ignore: type => type === 'unknown-flag'
    }
)

// $ node ./cli --unknown=hello
parsed._ // => []
argv // => ['--unknown=hello']

Ignoring everything after the first argument

Similarly to how Node.js only reads flags passed in before the first argument, type-flag can be configured to ignore everything after the first argument.

const argv = process.argv.slice(2)

let stopParsing = false
const parsed = typeFlag(
    {
        myFlag: [Boolean]
    },
    argv,
    {
        ignore(type) {
            if (stopParsing) {
                return true
            }
            const isArgument = type === 'argument'
            if (isArgument) {
                stopParsing = isArgument
                return stopParsing
            }
        }
    }
)

// $ node ./cli --my-flag ./file.js --my-flag
parsed.flags.myFlag // => [true]
argv // => ['./file.js', '--my-flag']

👨🏻‍🏫 Examples

Custom flag type

Basic types can be set using built-in functions in JavaScript, but sometimes you want to a new type, narrow the type, or add validation.

To create a new type, simply declare a function that accepts a string argument and returns the parsed value with the expected type.

In this example, the size flag is enforced to be either small, medium or large.

const possibleSizes = ['small', 'medium', 'large'] as const

type Sizes = typeof possibleSizes[number]

function Size(size: Sizes) {
    if (!possibleSizes.includes(size)) {
        throw new Error(`Invalid size: "${size}"`)
    }

    return size
}

const parsed = typeFlag({
    size: Size
})

parsed resolves to the following type:

type Parsed = {
    flags: {
        size: 'small' | 'medium' | 'large' | undefined
    }
    // ...
}

Optional value flag

To create a string flag that acts as a boolean when nothing is passed in, create a custom type that returns both types.

function OptionalString(value: string) {
    if (!value) {
        return true
    }

    return value
}

const parsed = typeFlag({
    string: OptionalString
})

// $ node ./cli --string
parsed.flags.string // => true

// $ node ./cli --string hello
parsed.flags.string // => 'hello'

Accepting flag values with = in it

In use-cases where flag values contain =, you can use : instead. This allows flags like --define:K=V.

const parsed = typeFlag({
    define: String
})

// $ node ./cli --define:key=value
parsed.flags.define // => 'key=value'

Dot-nested flags

type Environment = {
    TOKEN?: string
    CI?: boolean
}

function EnvironmentObject(value: string): Environment {
    const [propertyName, propertyValue] = value.split('=')
    return {
        [propertyName]: propertyValue || true
    }
}

const parsed = typeFlag({
    env: [EnvironmentObject]
})

const env = parsed.flags.env.reduce(
    (agg, next) => Object.assign(agg, next),
    {}
)

// $ node ./cli --env.TOKEN=123 --env.CI
env // => { TOKEN: 123, CI: true }

Inverting a boolean

To invert a boolean flag, false must be passed in with the = operator (or any other value delimiters).

const parsed = typeFlag({
    booleanFlag: Boolean
})

// $ node ./cli --boolean-flag=false
parsed.flags.booleanFlag // => false

Without explicitly specfying the flag value via =, the false will be parsed as a separate argument.

// $ node ./cli --boolean-flag false
parsed.flags.booleanFlag // => true
parsed._ // => ['false']

Counting flags

To create an API where passing in a flag multiple times increases a count (a pretty common one is -vvv), you can use an array-boolean type and count the size of the array:

const parsed = typeFlag({
    verbose: {
        type: [Boolean],
        alias: 'v'
    }
})

// $ node ./cli -vvv
parsed.flags.verbose.length // => 3

⚙️ API

typeFlag(flagSchema, argv, options)

Returns an object with the shape:

type Parsed = {
    flags: {
        [flagName: string]: InferredType
    }
    unknownFlags: {
        [flagName: string]: (string | boolean)[]
    }
    _: string[]
}

flagSchema

Type:

type TypeFunction = (argvValue: any) => any

type FlagSchema = {
    [flagName: string]: TypeFunction | [TypeFunction] | {
        type: TypeFunction | [TypeFunction]
        alias?: string
        default?: any
    }
}

An object containing flag schema definitions. Where the key is the flag name, and the value is either the type function or an object containing the type function and/or alias.

argv

Type: string[]

Default: process.argv.slice(2)

The argv array to parse. The array is mutated to remove the parsed flags.

options

Type:

type Options = {
    // Callback to skip parsing on certain argv tokens
    ignore?: (
        type: 'known-flag' | 'unknown-flag' | 'argument',
        flagOrArgv: string,
        value: string | undefined,
    ) => boolean | void
}

getFlag(flagNames, flagType, argv)

flagNames

Type: string

A comma-separated list of flag names to parse.

flagType

Type:

type TypeFunction = (argvValue: any) => any

type FlagType = TypeFunction | [TypeFunction]

A function to parse the flag value. Wrap the function in an array to retrieve all values.

argv

Type: string[]

Default: process.argv.slice(2)

The argv array to parse. The array is mutated to remove the parsed flags.

Sponsors

More Repositories

1

tasuku

✅ タスク — The minimal task runner for Node.js
TypeScript
1,687
star
2

minification-benchmarks

🏃‍♂️🏃‍♀️🏃 JS minification benchmarks: babel-minify, esbuild, terser, uglify-js, swc, google closure compiler, tdewolff/minify
TypeScript
830
star
3

cleye

👁‍🗨 cleye — The intuitive & typed CLI development tool for Node.js
TypeScript
361
star
4

snap-tweet

Snap a screenshot of a tweet 📸
TypeScript
312
star
5

vue-2-3

↔️ Interop Vue 2 components in Vue 3 apps and vice versa
JavaScript
277
star
6

pkgroll

📦 🍣 Next-gen package bundler for TypeScript & ESM
TypeScript
249
star
7

ts-runtime-comparison

Comparison of Node.js TypeScript runtimes
TypeScript
219
star
8

vue-frag

🤲 Create Fragments (multiple root-elements) in Vue 2
TypeScript
194
star
9

link

🔗 A better `npm link`
TypeScript
117
star
10

get-tsconfig

tsconfig.json paser & paths matcher
TypeScript
91
star
11

github-cdn

🛰 Github CDN Server
JavaScript
47
star
12

ci

Run npm ci using the appropriate Node package manager (npm, yarn, pnpm)
TypeScript
45
star
13

dbgr

Lightweight debugger for Node.js
TypeScript
44
star
14

clean-pkg-json

Script to remove unnecessary properties from package.json on prepublish hook
TypeScript
41
star
15

vue-frag-plugin

Webpack/Rollup/Vite plugin to add multiple root-node support to Vue 2 SFCs
TypeScript
41
star
16

instant-mocha

☕️ Build tests with Webpack and run with Mocha in one command
TypeScript
41
star
17

resolve-pkg-maps

Resolve package.json `exports` & `imports` maps
TypeScript
39
star
18

git-publish

☁️ Publish your npm package to a GitHub repository branch
TypeScript
38
star
19

compare-bun-node

Comparison of Bun's API against Node.js's
JavaScript
38
star
20

VisualQuery

Light & minimal front-end query builder for jQuery
JavaScript
38
star
21

webpack-localize-assets-plugin

🌐 Localize your Webpack bundle with multiple locales
TypeScript
38
star
22

webpack-json-access-optimizer

Webpack plugin to tree-shake and minify JSON modules
TypeScript
37
star
23

terminal-columns

Render readable & responsive tables in the terminal
TypeScript
32
star
24

deps

📦🔍 Analyze which package.json dependencies are in-use with V8 Coverage 🔥
TypeScript
31
star
25

vue-pseudo-window

🖼 Declaratively interface window/document/body in your Vue template
JavaScript
31
star
26

manten

💮 満点 - Lightweight testing library for Node.js
TypeScript
29
star
27

svg-browser-export

Export SVG to PNG, JPEG, or WEBP in the browser
TypeScript
23
star
28

comment-mark

Interpolate strings with HTML comment markers!
TypeScript
23
star
29

npm-multi-publish

Publish an npm package to multiple registries
JavaScript
23
star
30

reactive-json-file

💎 Reactively sync JSON mutations to disk
TypeScript
23
star
31

playwright-start

Start a long-running Playwright browser server via CLI
TypeScript
22
star
32

ink-task-list

Task runner components for Ink
TypeScript
22
star
33

chainset

Set object values using property chaining syntax
TypeScript
21
star
34

build-this-branch

🤖 Script to automate creating built branches
TypeScript
21
star
35

vue-vnode-syringe

🧬 Add attributes and event-listeners to <slot> content 💉
JavaScript
20
star
36

vue-dom-hints

Vue.js devtool for identifying Vue components and their SFC paths in the DOM
JavaScript
20
star
37

vue-grep

Grep your Vue.js codebase with CSS selectors
TypeScript
19
star
38

git-squash-branch

Script to squash the commits in the current Git branch
TypeScript
17
star
39

fs-fixture

Easily create test fixtures at a temporary file-system path
TypeScript
16
star
40

vue-split-view

Create a resizable split-view to partition the UI
Vue
16
star
41

alias-imports

Create Node.js aliases via imports map
TypeScript
16
star
42

vue-import-loader

🌐 Webpack loader to automatically detect & import used components
JavaScript
13
star
43

fs-require

Create a require() function from any file-system. Great for in-memory fs testing!
TypeScript
13
star
44

pkg-entry-points

Get all entry-points for an npm package. Supports the `exports` field in `package.json`
TypeScript
13
star
45

vue-subslot

💍 Pick out specific elements from the component <slot>
JavaScript
12
star
46

gh-emojis

Use GitHub emojis from their API as an npm package
JavaScript
12
star
47

litemark

🖋 GitHub Flavored Markdown Editor
Vue
12
star
48

hirok.io

👤 My personal website & blog
Vue
11
star
49

vue-proxi

💠 Tiny proxy component for Vue.js
JavaScript
11
star
50

bfs

Find the path of a value in a complex JavaScript object graph/tree.
JavaScript
11
star
51

prerelease-checks

Run essential pre-release checks before releasing an npm package
TypeScript
11
star
52

git-detect-case-change

🤖 Script to detect file name case changes in a Git repository
TypeScript
10
star
53

cli-simple-table

Simple CLI table for simple people
TypeScript
9
star
54

webpack-test-utils

Utility functions to test Webpack loaders/plugins
TypeScript
8
star
55

vue-svg-icon-set

Efficiently expose a SVG icon set in Vue
JavaScript
8
star
56

webpack-playground

Demonstration of different Webpack configurations
JavaScript
8
star
57

eslint-config

TypeScript
7
star
58

vue-v

Tiny component to render Vue.js vNodes in the template.
JavaScript
7
star
59

i-peers

npx package to install local peer-dependencies
JavaScript
7
star
60

fs.promises.exists

🪐 The missing fs.promises.exists(). Also supports case-sensitive/insensitive file paths.
TypeScript
7
star
61

vue-just-ssr

🔥 Instantly add a Vue SSR dev-env to your Webpack build
JavaScript
7
star
62

issue-reproductions

A repository to collect and organize my issue reproductions
JavaScript
6
star
63

webpack-analyze-duplication-plugin

Detect duplicated modules in your Webpack build
JavaScript
6
star
64

vue-feather-icon-set

Optimized Feather icon set for Vue using SVG references
JavaScript
6
star
65

is-fs-case-sensitive

Check whether the file-system is case-sensitive
TypeScript
6
star
66

npm-registry-sync

A daemon to sync packages across npm registries
TypeScript
6
star
67

babel-plugin-debug-object-location

Babel plugin to help you determine where an object or array was instantiated
TypeScript
5
star
68

generate-batched-pr-manifest

Generate a manifest of all the PRs included in a batched PR
TypeScript
5
star
69

entry-file-plugin

Create an ESM entry-file in your Webpack build to consolidate entry-point exports
TypeScript
5
star
70

privatenumber

5
star
71

webpack-dependency-size

👩‍🔬 Webpack plugin to get an overview of bundled dependencies and their size
JavaScript
5
star
72

vue-demo-collapse

Vue component that shows a demo and a "Show Code" button to expand source code
Vue
4
star
73

markdown-it-add-attrs

markdown-it plugin to add attributes to all rendered elements
JavaScript
4
star
74

browser-reload-plugin

Automatically reload the browser page on every Webpack watch build
JavaScript
4
star
75

rollup-playground

Demonstration of different Rollup configurations
JavaScript
3
star
76

rollup-plugin-htmlvue

Rollup plugin for transforming HTML/XML to Vue SFC
TypeScript
3
star
77

htmlvue-loader

Webpack loader for compiling HTML to Vue
JavaScript
3
star
78

vue-ast-utils

Utils for working with Vue 3 AST nodes
TypeScript
3
star
79

systemjs-unpkg

SystemJS extra to auto-resolve bare specifiers to UNPKG
JavaScript
3
star
80

postcss-custom-properties-transformer

PostCSS plugin to apply transformations to CSS custom properties (eg. mangling)
JavaScript
2
star
81

web-diff

Visually diff web pages
JavaScript
2
star
82

rollup-plugin-aggregate-exports

Emit an entry file to aggregate exports across multiple files.
JavaScript
2
star
83

svg-trace-loader

Webpack loader to trace and flatten SVGs into one path
JavaScript
2
star
84

repkg

🚚 On-demand AMD npm package bundling service
JavaScript
2
star
85

webpack-distsize

Track Webpack output size via version control
JavaScript
2
star
86

motion-orientation-api

Motion & Orientation API explorer
Vue
2
star
87

vue-server-renderer

JavaScript
2
star
88

postcss-import-alias

Use aliases in your PostCSS import statements
JavaScript
2
star
89

pm2-no-daemon-bug

JavaScript
1
star
90

resume

1
star
91

fs-router-patch

Add routing for shimming fs access
JavaScript
1
star
92

gulp-watchman

JavaScript
1
star
93

.github

1
star
94

sharp-vs-resvgjs

JavaScript
1
star
95

mempack

Run a Webpack build in-memory
JavaScript
1
star