https://github.com/ponlawat-w/flags-args-readline
Command line arguments reader from specified flags, with readline prompt when value is not specified.
https://github.com/ponlawat-w/flags-args-readline
arguments arguments-reader cmd flags npm parser reader
Last synced: about 1 year ago
JSON representation
Command line arguments reader from specified flags, with readline prompt when value is not specified.
- Host: GitHub
- URL: https://github.com/ponlawat-w/flags-args-readline
- Owner: ponlawat-w
- Created: 2020-02-13T15:49:25.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-13T17:06:07.000Z (over 6 years ago)
- Last Synced: 2025-03-09T13:36:29.216Z (over 1 year ago)
- Topics: arguments, arguments-reader, cmd, flags, npm, parser, reader
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/flags-args-readline
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# flag-args
Command line arguments reader from specified flags, with readline prompt when value is not specified.
## 1) Arguments Reader
### Initialisation
```js
// Initialise from all function objects
const FlagArgs = require('flags-args-readline');
const args = FlagArgs.readArgs(['-f', '--file', '-t', '--target']);
// Initialise from only `readArgs` object
const { readArgs } = require('flags-args-readline');
const args = readArgs(['-f', '--file', '-t', '--target']);
```
### Methods
#### 1. `has(params: string[]|string): bool`
Return `true` if there is value of that given name.
**Examples**
```js
/* COMMAND LINE:
node test.js --target ./data.txt -a
*/
const args = readArgs(['-t', '--target', '-a']);
args.has('-t'); // false
args.has('--target'); // true
args.has(['-t', '--target']); // true
args.has('-a'); // false
```
#### 2. `get(params: string[]|string): string`
Return value of specified argument name.
**Examples**
```js
/* COMMAND LINE:
node test.js --source ./data.txt -t ./out.txt
*/
const args = readArgs(
['-s', '--source', '-t', '--target']
);
args.get(['-s', '--source']); // "./data.txt"
args.get(['-t', '--target']); // "./out.txt"
args.get('-t'); // "./out.txt"
args.get('--target'); // undefined
```
#### 3. `getOrReadline(params: string[]|string): string`
If there is value given in command line, return the value. Otherwise, show interactive line reader and read line from user's input, then return the value.
**Examples**
```js
/* COMMAND LINE:
node test.js -s ./data.txt
*/
const args = readArgs(
['-s', '--source', '-t', '--target']
);
temp = args.getOrReadline(['-s', '--source'], 'Where is source file?: ');
// ↑ "./data.txt"
temp = args.getOrReadline(['-t', '--target'], 'Where is target file?: ');
// ↑ Pause program, print "Where is target file?: " and then wait for user to input a line. After user has submit, read the line and return the value.
args.has(['-t', '--target']); // false
// ↑ using `getOrReadline` will not change aruments records in `args`, the value from readline is one-time use.
```
### Accessing Values Directly
**Examples**
```js
/* COMMAND LINE:
node test.js -t ./out.txt
*/
const args = readArgs(
['-t', '--target', '-s', '--source']
);
args.args['-t']; // "./out.txt"
args.args['--target']; // undefined
args.args['-s']; // undefined
args.args['--source']; // undefined
```
---
## 2) Flags Reader
### Initialisation
```js
// Initialise from all function objects
const FlagArgs = require('flags-args-readline');
const args = FlagArgs.readFlags(['-a', '--all']);
// Initialise from only `readFlags` object
const { readFlags } = require('flags-args-readline');
const args = readFlags(['-a', '--all']);
```
### Methods
#### `has(string[]|string): bool`
Return `true` if there is specified flag given from command line arguments. The logical operator, in case of array, is OR.
**Examples**
```js
/* COMMAND LINE:
node test.js -a
*/
const flags = readFlags(['-a', '--all']);
flags.has(['-a', '--all']); // true
flags.has('-a'); // true
flags.has('--all'); // false
flags.has('-t'); // false
```
### Accessing Values Directly
**Examples**
```js
/* COMMAND LINE:
node test.js -a -u -t
*/
const flags = readFlags(['-a', '-u', '-s']);
flags.flags; // ['-a', '-u']
```
---
## Using with `npm` command
Reminding that arguments passed from `npm` commands, such as `npm start` or `npm run`, those arguments belong to `npm`, not the node file. For this reason, to use the library with `npm`, the arguments should be passed after `--` symbol.
**Examples**
```js
const args = parseArgs(['-s', '-t']);
args.get('-s');
// From `npm run my-command -s something` → undefined
// From `node run my-command -- -s something` → "something"
```
---