Package Exports
- command-handling
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (command-handling) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
command-handling
The lightweight Node.js command handling that is using for CLI app. This package is also using for code-template-generator.
This package is using for small app that is not has many complex features. You can view Commander.js if you are thinking about a great Node.js CLI app.
Table of contents
Introduction
command-handling
help you to analyse the input command line. It catches the arguments that you may be waiting for then you decide what you want to do with the raw data after parsing.
Command line structure
The simple command line structure that is used in this package:
$ command [-option][--alias] [--sub-option] [argument]
- An option has only an alias.
- An option has many sub options.
- Use cases:
command
(only command without anything)command [argument]
command [-option][--alias]
command [-option][--alias] [argument]
command [-option][--alias] [--sub-option]
command [-option][--alias] [--sub-option] [argument]
command [--sub-option] [argument]
- View examples for code-template-generator to know more about use cases.
Methods
Method | Argument | Description |
---|---|---|
.option() |
<flag>, [alias], [description] |
Option definition |
.subOption() |
<mainFlag>, <subFlag>, description |
Sub option definition |
.showOptions() |
- | It returns an options array |
.parse() |
<process.argv> |
Parse the command line |
Data structures
- An object is returned when you call method
.parse(process.argv)
. You need this object for your coding. View example for more detail.
{ // Result structure
mainFlag: null,
subFlags: [],
argument: null,
commandLength: 0,
unknowns: []
}
- An array is returned when you call method
.showOptions()
. You may want to have the option list for your help function.
[ // All options
{ // Option 1
flag: '', // main flag
alias: '',
description: '',
subFlags: [
{
flag: '', // sub flag 1
description: ''
},
{
flag: '', // sub flag n
description: ''
}
]
},
{ // Option n
...
}
]
Using
- Step 1:
const { Command } = require('command-handling')
- Step 2:
const command = new Command()
- Step 3: You can use method chaining for
command
object.- Option definition:
.option(<flag>, [alias], [description])
- Sub option definition:
.subOption(<mainFlag>, <subFlag>, [description])
- Option definition:
- Step 4:
.parse(process.argv)
-> It is in the the end of chaining. You can use also this way:const result = command.parse(process.argv)
-> It returns an object after parsing. You need this object for your coding.
- Extra: Using method
.showOptions()
to get back all options are defined above for your help function.- Example:
const optionList = command.showOptions()
- Example:
Example
const { Command } = require('command-handling');
const command = new Command();
command
.option("-v", "--version", "View the installed version")
.option("-help", "--help", "View the help information")
.option("-cf", "--config", "Config for this app")
.subOption("-cf", "--set-asset", "Store the asset directory path")
.subOption("-cf", "--view-asset", "View the asset directory path")
.parse(process.argv);
const optionList = command.showOptions();
console.log(optionList); // It returns an array with all defined options
View more examples on here.
Thank you!
Many thanks to Commander.js for the inspiration.
(Completed document is comming soon)