├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.js ├── test.js ├── package.json ├── license └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | - 16 16 | - 14 17 | - 12 18 | steps: 19 | - uses: actions/checkout@v3 20 | - uses: actions/setup-node@v3 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - run: npm install 24 | - run: npm test 25 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import execall from 'execall'; 2 | 3 | export default function parseHelp(string) { 4 | const returnValue = { 5 | flags: {}, 6 | aliases: {} 7 | }; 8 | 9 | const regex = /^\s*[^$]\s*(?:-([a-z-]),[ \t]+)?--([a-z-]+) +(.*)$/gim; 10 | 11 | for (const {subMatches} of execall(regex, string)) { 12 | const [shortFlag, longFlag, description] = subMatches; 13 | 14 | if (shortFlag) { 15 | returnValue.aliases[shortFlag] = longFlag; 16 | } 17 | 18 | if (longFlag) { 19 | returnValue.flags[longFlag] = {}; 20 | 21 | const flag = returnValue.flags[longFlag]; 22 | 23 | if (shortFlag) { 24 | flag.alias = shortFlag; 25 | } 26 | 27 | if (description) { 28 | flag.description = description; 29 | } 30 | } 31 | } 32 | 33 | return returnValue; 34 | } 35 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import parseHelp from './index.js'; 3 | 4 | const fixture = ` 5 | Usage 6 | $ unicorn 7 | 8 | Options 9 | --rainbow Lorem ipsum dolor sit amet 10 | -m, --magic Aenean commodo ligula eget dolor 11 | --pony Nullam dictum felis eu pede 12 | -c, --color Donec quam felis 13 | -h, --help Felis quam cenod 14 | 15 | 16 | Examples 17 | $ unicorn Peachy 18 | $ unicorn Sparkle --rainbow --magic 19 | `; 20 | 21 | test('main', t => { 22 | const parsed = parseHelp(fixture); 23 | t.deepEqual(Object.keys(parsed.flags), ['rainbow', 'magic', 'pony', 'color', 'help']); 24 | t.is(parsed.flags.rainbow.description, 'Lorem ipsum dolor sit amet'); 25 | t.is(parsed.flags.magic.alias, 'm'); 26 | t.is(parsed.aliases.m, 'magic'); 27 | t.is(parsed.aliases.c, 'color'); 28 | t.is(parsed.aliases.h, 'help'); 29 | }); 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "parse-help", 3 | "version": "2.0.0", 4 | "description": "Parse CLI help output", 5 | "license": "MIT", 6 | "repository": "sindresorhus/parse-help", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava" 20 | }, 21 | "files": [ 22 | "index.js" 23 | ], 24 | "keywords": [ 25 | "parse", 26 | "help", 27 | "cli", 28 | "argv", 29 | "command-line", 30 | "man", 31 | "flags", 32 | "arguments", 33 | "alias", 34 | "aliases", 35 | "short" 36 | ], 37 | "dependencies": { 38 | "execall": "^3.0.0" 39 | }, 40 | "devDependencies": { 41 | "ava": "^3.15.0", 42 | "xo": "^0.38.2" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # parse-help 2 | 3 | > Parse CLI help output 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install parse-help 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import parseHelp from 'parse-help'; 15 | 16 | const help = ` 17 | Usage 18 | $ unicorn 19 | 20 | Options 21 | --rainbow Lorem ipsum dolor sit amet 22 | -m, --magic Aenean commodo ligula eget dolor 23 | --pony Nullam dictum felis eu pede 24 | -c, --color Donec quam felis 25 | 26 | Examples 27 | $ unicorn Peachy 28 | $ unicorn Sparkle --rainbow --magic 29 | `; 30 | 31 | parseHelp(help); 32 | /* 33 | { 34 | flags: { 35 | rainbow: { 36 | description: 'Lorem ipsum dolor sit amet' 37 | }, 38 | magic: { 39 | alias: 'm', 40 | description: 'Aenean commodo ligula eget dolor' 41 | }, 42 | pony: { 43 | description: 'Nullam dictum felis eu pede' 44 | }, 45 | color: { 46 | alias: 'c', 47 | description: 'Donec quam felis' 48 | } 49 | }, 50 | aliases: { 51 | m: 'magic', 52 | c: 'color' 53 | } 54 | } 55 | */ 56 | ``` 57 | 58 | ## Related 59 | 60 | - [aliases](https://github.com/sindresorhus/aliases) - Parse flag aliases in CLI help output 61 | --------------------------------------------------------------------------------