├── .prettierrc ├── mod.ts ├── .gitignore ├── .vscode └── settings.json ├── renovate.json ├── tsconfig.json ├── CHANGELOG.md ├── LICENSE ├── package.json ├── .github └── workflows │ └── ci.yml ├── README.md ├── test └── test.ts ├── src └── index.ts └── pnpm-lock.yaml /.prettierrc: -------------------------------------------------------------------------------- 1 | "@egoist/prettier-config" 2 | -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | export * from "./src/index.ts" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | dist 4 | *.log 5 | coverage -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true 3 | } 4 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2020", 4 | "module": "esnext", 5 | "strict": true, 6 | "esModuleInterop": true, 7 | "moduleResolution": "node", 8 | "skipLibCheck": true, 9 | "noUnusedLocals": true, 10 | "noImplicitAny": true, 11 | "allowJs": true, 12 | "resolveJsonModule": true 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.1.4 2 | 3 | - Allow `meta` property on `option`: 4 | 5 | ```ts 6 | parse<{ description: string }>([ 7 | { name: "foo", type: String, meta: { description: "foo flag" } }, 8 | ]) 9 | ``` 10 | 11 | This is useful if you want to build a help message output options, you can pass additional properties to `meta` to store some information, like description. 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2021 EGOIST (https://github.com/sponsors/egoist) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tinyargs", 3 | "version": "0.1.4", 4 | "description": "A tiny and flexible command-line argument parser for Node.js and Deno.", 5 | "publishConfig": { 6 | "access": "public" 7 | }, 8 | "files": [ 9 | "dist" 10 | ], 11 | "main": "./dist/index.js", 12 | "module": "./dist/index.mjs", 13 | "exports": { 14 | "require": "./dist/index.js", 15 | "import": "./dist/index.mjs" 16 | }, 17 | "types": "./dist/index.d.ts", 18 | "scripts": { 19 | "build-fast": "tsup src/index.ts --format cjs,esm --target node14", 20 | "build": "pnpm run build-fast -- --dts-resolve", 21 | "test": "pnpm run build && ava", 22 | "test-cov": "c8 -r lcov npm run test", 23 | "prepublishOnly": "pnpm run build-fast", 24 | "ts": "node -r sucrase/register" 25 | }, 26 | "license": "MIT", 27 | "devDependencies": { 28 | "@egoist/prettier-config": "1.0.0", 29 | "ava": "4.0.0-rc.1", 30 | "c8": "7.11.0", 31 | "prettier": "2.5.1", 32 | "sucrase": "3.20.3", 33 | "tsup": "5.11.10", 34 | "typescript": "4.5.4", 35 | "uvu": "0.5.2" 36 | }, 37 | "engines": { 38 | "node": ">=14" 39 | }, 40 | "ava": { 41 | "extensions": [ 42 | "ts" 43 | ], 44 | "require": [ 45 | "sucrase/register" 46 | ] 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | test: 11 | if: "!contains(github.event.head_commit.message, 'ci skip')" 12 | 13 | strategy: 14 | matrix: 15 | os: [ubuntu-latest] 16 | node-version: [14.x] 17 | 18 | runs-on: ${{ matrix.os }} 19 | 20 | # Steps represent a sequence of tasks that will be executed as part of the job 21 | steps: 22 | # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it 23 | - uses: actions/checkout@v2 24 | 25 | - uses: actions/setup-node@v2 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | 29 | - name: Cache ~/.pnpm-store 30 | uses: actions/cache@v2 31 | env: 32 | cache-name: cache-pnpm-store 33 | with: 34 | path: ~/.pnpm-store 35 | key: ${{ runner.os }}-${{ matrix.node-version }}-test-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }} 36 | restore-keys: | 37 | ${{ runner.os }}-${{ matrix.node-version }}-test-${{ env.cache-name }}- 38 | ${{ runner.os }}-${{ matrix.node-version }}-test- 39 | ${{ runner.os }}- 40 | 41 | - name: Install pnpm 42 | run: npm i -g pnpm 43 | 44 | - name: Install deps 45 | run: pnpm i 46 | 47 | # Runs a set of commands using the runners shell 48 | - name: Build and Test 49 | run: pnpm run test-cov 50 | 51 | - name: Upload Coverage 52 | if: ${{ matrix.os == 'ubuntu-latest' }} 53 | uses: coverallsapp/github-action@master 54 | with: 55 | github-token: ${{ secrets.GITHUB_TOKEN }} 56 | path-to-lcov: ./coverage/lcov.info 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).** 2 | 3 | --- 4 | 5 | # tinyargs 6 | 7 | [![npm version](https://badgen.net/npm/v/tinyargs)](https://npm.im/tinyargs) [![npm downloads](https://badgen.net/npm/dm/tinyargs)](https://npm.im/tinyargs) [![Coverage Status](https://coveralls.io/repos/github/egoist/tinyargs/badge.svg?branch=main)](https://coveralls.io/github/egoist/tinyargs?branch=main) 8 | 9 | A tiny and flexible command-line argument parser for Node.js and Deno. 10 | 11 | ## Features 12 | 13 | - Support combined short flags, `-abc foo` is expanded to `-a -b -c foo` 14 | - Support using equal sign to specify argument value, `-c=config.js` is expanded to `-c config.js` 15 | - Support positional arguments like `your-cli foo bar` 16 | - Support collecting trailing arguments 17 | - Support sub commands 18 | 19 | ## Install 20 | 21 | ```bash 22 | npm i tinyargs 23 | ``` 24 | 25 | [Deno](https://deno.land) users: 26 | 27 | ```ts 28 | import { parse } from "https://deno.land/x/tinyargs/mod.ts" 29 | ``` 30 | 31 | ## Examples 32 | 33 | ### Simple Example 34 | 35 | ```ts 36 | import { parse } from "tinyargs" 37 | 38 | const cli = parse(process.argv.slice(2), [ 39 | { name: "help", flags: ["h"], type: Boolean, stop: true }, 40 | { name: "version", flags: ["v"], type: Boolean, stop: true }, 41 | { name: "files", type: String, positional: true, multiple: true }, 42 | ]) 43 | 44 | if (cli.help) { 45 | console.log(`...your help message`) 46 | process.exit() 47 | } 48 | 49 | if (cli.version) { 50 | console.log(`...version number`) 51 | process.exit() 52 | } 53 | 54 | console.log(cli.files) 55 | ``` 56 | 57 | Run this cli: 58 | 59 | ```bash 60 | $ node cli.js -h 61 | ...your help message 62 | 63 | $ node cli.js -v 64 | ...version number 65 | 66 | $ node cli.js foo.js bar.js 67 | [ 'foo.js', 'bar.js' ] 68 | 69 | $ node cli.js 70 | error: missing positional argument: files 71 | ``` 72 | 73 | If `-h, --help` or `-v, --version` appears, the remaining arguments are not parsed, since we added `stop: true` to the option. 74 | 75 | By default all options and positional arguments are required to have a value, if you add a string option named `foo` but it's used like `--foo --bar`, it will throw an error. This can be customized by setting `optionalValue: true`, which in this case would give `foo` a default value of `true` instead. 76 | 77 | ### Sub Commands 78 | 79 | Create a CLI with two sub commands: (_We use `<>` and `[]` to denote cli arguments in the docs, `<>` means it's required, `[]` means it's optional._) 80 | 81 | - `run