Skip to content

Latest commit

 

History

History
1614 lines (1025 loc) · 47.5 KB

API.md

File metadata and controls

1614 lines (1025 loc) · 47.5 KB

Classes

AbstractCLI

Defines the shape of the root CLI including title, description, usage, help, and options/switches. Additionally this contains all of the functionality of running a CLI such as init, parseOptions, and run methods.

This class is the base class for AwesomeCLI.CLI and AwesomeCLI.CommandCLI.

AbstractCommandAbstractCLI

Defines the shape of a command, be it a sub-command, or the root command overloaded from AwesomeCLI.CLI or AwesomeCLI.CommandCLI. While root commands should inherit from AwesomeCLI.CLI or AwesomeCLI.CommandCLI any sub-commands below those would inherit directly from AbstractCommand.

AbstractCommand inherits from AbstractCLI and thus has access to all of its methods such as title, usage, desriptino, addOption, addOptionShortcut, etc.

BadCommandErrorError

Thrown when a command fails to compile or execute properly.

CLIAbstractCLI

The root command for a singular CLI that is not using sub-commands.

CLIUtils

A selection of utilities to speed CLI development.

CommandCLIAbstractCommand

THe root command for a command based CLI that uses sub-commands.

InvalidCommandErrorError

Thrown if a command that is being added is not a Function, AbstractCommand, or filename string.

InvalidOptionErrorError

Thrown when an option is used when executing the CLI but that option does not exist or is otherwise not allowed.

InvalidOptionValueErrorError

Thrown when a user of the CLI passes in a value to the CLI that does not match the given type for the option, for example passing a string when a number is required.

InvalidTypeErrorError

Thrown when an option is being added but the given type is not "*" or "string" or "number" or "boolean".

MissingCommandErrorError

Thrown when a command CLI gets a command it does not have a command mapped to.

MissingOptionValueErrorError

Thrown when a user of the CLI gives a specific option but does not follow it with a required argument.

Constants

AbstractCLI

Defines the AwesomeCLI main object, whcih returns pointer to the various AwesomeCLI classes.

AbstractCLI

Defines the shape of the root CLI including title, description, usage, help, and options/switches. Additionally this contains all of the functionality of running a CLI such as init, parseOptions, and run methods.

This class is the base class for AwesomeCLI.CLI and AwesomeCLI.CommandCLI.

Kind: global class


new AbstractCLI()

Constructs a new CLI.


abstractCLI.title ⇒ string

Returns the title of this root CLI. Should be overloaded by extending classes.

Kind: instance property of AbstractCLI


abstractCLI.description ⇒ string

Returns the description of this root CLI. Should be overloaded by extending classes.

Kind: instance property of AbstractCLI


abstractCLI.usage ⇒ string

Returns the usage of this root CLI. Should be overloaded by sxtending classes.

Kind: instance property of AbstractCLI


abstractCLI.help() ⇒ void

The default help implementation for a root CLI. Overload this if you want to customize your help display.

Kind: instance method of AbstractCLI


abstractCLI.init(command, parsed) ⇒ Object

Called by the run() method to initiate the root command and option parsing. Generally this doesn't need to be overloaded.

Kind: instance method of AbstractCLI
Returns: Object - the parsed arguments and options.

Param Type Description
command Array line arguments.
parsed Object options and their values.

abstractCLI.run(command, parsed) ⇒ Promise

Called when you start your CLI, this takes care of initializing, parsing arguments, and ensuring everything is run smoothly.

By and large there is zero reason to overload this. You should overload execute(args,options), before(args,options), or after(args,options) instead.

Kind: instance method of AbstractCLI

Param Type Description
command Array line arguments.
parsed Object options and their values.

abstractCLI.getOption(optionName) ⇒ Object

Returns the object used to describe an option after it has been added.

Kind: instance method of AbstractCLI

Param Type
optionName string

abstractCLI.getOptions() ⇒ Array.<string>

Returns an array of all the option names that have been added.

Kind: instance method of AbstractCLI


abstractCLI.addOption(optionName, [type], [defaultValue], [description])

Adds a new options.

optionName must be unique to this command and any descendant commands.

type can be "*", "boolean", "number", or "string".

defaultvalue is the value used if the option is not prvided.

description is the help description for this option.

Kind: instance method of AbstractCLI

Param Type Default
optionName string
[type] string "&quot;*&quot;"
[defaultValue] *
[description] string "&quot;&quot;"

abstractCLI.removeOption(optionName) ⇒ void

Removes a given option.

Kind: instance method of AbstractCLI

Param Type
optionName string

abstractCLI.getOptionShortcut(shortcut) ⇒ string

Returns the Shortcut object for a given shortcut.

Kind: instance method of AbstractCLI

Param Type
shortcut string

abstractCLI.getOptionShortcuts() ⇒ Array.<string>

Returns an array of all the shortcut names added.

Kind: instance method of AbstractCLI


abstractCLI.addOptionShortcut(shortcut, optionName)

Adds a new option shortcut which maps the shortcut to a specific optionName.

Kind: instance method of AbstractCLI

Param Type
shortcut string
optionName string

abstractCLI.removeOptionShortcut(shortcut) ⇒ void

Removes an option shortcut.

Kind: instance method of AbstractCLI

Param Type
shortcut string

abstractCLI.parseOptions(command, initialOptions) ⇒ Object

Responsible for parsing the command line arguments and determining what is an option and what is an argument.

Generally there is no reason to overload this.

Kind: instance method of AbstractCLI

Param Type Description
command Array line arguments.
initialOptions Object

AbstractCommand ⇐ AbstractCLI

Defines the shape of a command, be it a sub-command, or the root command overloaded from AwesomeCLI.CLI or AwesomeCLI.CommandCLI. While root commands should inherit from AwesomeCLI.CLI or AwesomeCLI.CommandCLI any sub-commands below those would inherit directly from AbstractCommand.

AbstractCommand inherits from AbstractCLI and thus has access to all of its methods such as title, usage, desriptino, addOption, addOptionShortcut, etc.

Kind: global class
Extends: AbstractCLI


new AbstractCommand()

Constructs a new AbstractCommand.


abstractCommand.title ⇒ string

Returns the title of this AbstractCommand. Should be overloaded by extending classes.

Kind: instance property of AbstractCommand
Overrides: title


abstractCommand.description ⇒ string

Returns the description of this AbstractCommand. Should be overloaded by extending classes.

Kind: instance property of AbstractCommand
Overrides: description


abstractCommand.usage ⇒ string

Returns the usage of this AbstractCommand. Should be overloaded by extending classes.

Kind: instance property of AbstractCommand
Overrides: usage


abstractCommand.help() ⇒ void

The default help implementation for a AbstractCommand. Overload this if you want to customize your help display.

Kind: instance method of AbstractCommand
Overrides: help


abstractCommand.addCommand(name, command, description)

Add a new sub-command to this command.

name the name of the command, as it would be used.

command the command itself, which can be the following:

  • function commands, are simple functions that get passed the signature (args,options) when the command is executed. This approach is great for very simple "command CLIs".

  • filenane commands will, if the file exists and exports a sub-class of the AwesomeCLI.AbstractCommand class, load it as javascript, and create an instance of it. This lets you write sub-commands in their own class space and easily load them into AwesomeCLI. This is the best approach to doing commands and highly recommended.

    • AbstractCommmand commands, are similar to filename commands, but they dont do the loading as you are already providing the loaded instance to AwesomeCLI.

description Help descriptive text for this command.

Kind: instance method of AbstractCommand

Param Type
name string
command function | AbstractCommand | string
description string

abstractCommand.removeCommand(name) ⇒ void

Remove a command by name.

Kind: instance method of AbstractCommand

Param Type
name string

abstractCommand.getCommand(name) ⇒ Object

Get the command object for a given command name.

Kind: instance method of AbstractCommand

Param Type
name string

abstractCommand.getCommands() ⇒ Array.<string>

Returns an array of all command names for this particular command. Does not return commands defined by ancestor commands.

Kind: instance method of AbstractCommand


abstractCommand.run(args, options) ⇒ Promise

Executes the given command. Generally speaking you should not overload this function but instead overload execute(args,options) or before(args,options) or after(args,options).

Kind: instance method of AbstractCommand
Overrides: run

Param Type
args Array
options Object

abstractCommand.init(command, parsed) ⇒ Object

Called by the run() method to initiate the root command and option parsing. Generally this doesn't need to be overloaded.

Kind: instance method of AbstractCommand
Returns: Object - the parsed arguments and options.

Param Type Description
command Array line arguments.
parsed Object options and their values.

abstractCommand.getOption(optionName) ⇒ Object

Returns the object used to describe an option after it has been added.

Kind: instance method of AbstractCommand

Param Type
optionName string

abstractCommand.getOptions() ⇒ Array.<string>

Returns an array of all the option names that have been added.

Kind: instance method of AbstractCommand


abstractCommand.addOption(optionName, [type], [defaultValue], [description])

Adds a new options.

optionName must be unique to this command and any descendant commands.

type can be "*", "boolean", "number", or "string".

defaultvalue is the value used if the option is not prvided.

description is the help description for this option.

Kind: instance method of AbstractCommand

Param Type Default
optionName string
[type] string "&quot;*&quot;"
[defaultValue] *
[description] string "&quot;&quot;"

abstractCommand.removeOption(optionName) ⇒ void

Removes a given option.

Kind: instance method of AbstractCommand

Param Type
optionName string

abstractCommand.getOptionShortcut(shortcut) ⇒ string

Returns the Shortcut object for a given shortcut.

Kind: instance method of AbstractCommand

Param Type
shortcut string

abstractCommand.getOptionShortcuts() ⇒ Array.<string>

Returns an array of all the shortcut names added.

Kind: instance method of AbstractCommand


abstractCommand.addOptionShortcut(shortcut, optionName)

Adds a new option shortcut which maps the shortcut to a specific optionName.

Kind: instance method of AbstractCommand

Param Type
shortcut string
optionName string

abstractCommand.removeOptionShortcut(shortcut) ⇒ void

Removes an option shortcut.

Kind: instance method of AbstractCommand

Param Type
shortcut string

abstractCommand.parseOptions(command, initialOptions) ⇒ Object

Responsible for parsing the command line arguments and determining what is an option and what is an argument.

Generally there is no reason to overload this.

Kind: instance method of AbstractCommand

Param Type Description
command Array line arguments.
initialOptions Object

BadCommandError ⇐ Error

Thrown when a command fails to compile or execute properly.

Kind: global class
Extends: Error


CLI ⇐ AbstractCLI

The root command for a singular CLI that is not using sub-commands.

Kind: global class
Extends: AbstractCLI


new CLI()

Constructs a new CLI.


clI.title ⇒ string

Returns the title of this root CLI. Should be overloaded by extending classes.

Kind: instance property of CLI
Overrides: title


clI.description ⇒ string

Returns the description of this root CLI. Should be overloaded by extending classes.

Kind: instance property of CLI
Overrides: description


clI.usage ⇒ string

Returns the usage of this root CLI. Should be overloaded by sxtending classes.

Kind: instance property of CLI
Overrides: usage


clI.execute(args, options) ⇒ void | Promise

Overload execute(args,options) to do your work.

Kind: instance method of CLI

Param Type
args Array
options Object

clI.help() ⇒ void

The default help implementation for a root CLI. Overload this if you want to customize your help display.

Kind: instance method of CLI


clI.init(command, parsed) ⇒ Object

Called by the run() method to initiate the root command and option parsing. Generally this doesn't need to be overloaded.

Kind: instance method of CLI
Returns: Object - the parsed arguments and options.

Param Type Description
command Array line arguments.
parsed Object options and their values.

clI.run(command, parsed) ⇒ Promise

Called when you start your CLI, this takes care of initializing, parsing arguments, and ensuring everything is run smoothly.

By and large there is zero reason to overload this. You should overload execute(args,options), before(args,options), or after(args,options) instead.

Kind: instance method of CLI

Param Type Description
command Array line arguments.
parsed Object options and their values.

clI.getOption(optionName) ⇒ Object

Returns the object used to describe an option after it has been added.

Kind: instance method of CLI

Param Type
optionName string

clI.getOptions() ⇒ Array.<string>

Returns an array of all the option names that have been added.

Kind: instance method of CLI


clI.addOption(optionName, [type], [defaultValue], [description])

Adds a new options.

optionName must be unique to this command and any descendant commands.

type can be "*", "boolean", "number", or "string".

defaultvalue is the value used if the option is not prvided.

description is the help description for this option.

Kind: instance method of CLI

Param Type Default
optionName string
[type] string "&quot;*&quot;"
[defaultValue] *
[description] string "&quot;&quot;"

clI.removeOption(optionName) ⇒ void

Removes a given option.

Kind: instance method of CLI

Param Type
optionName string

clI.getOptionShortcut(shortcut) ⇒ string

Returns the Shortcut object for a given shortcut.

Kind: instance method of CLI

Param Type
shortcut string

clI.getOptionShortcuts() ⇒ Array.<string>

Returns an array of all the shortcut names added.

Kind: instance method of CLI


clI.addOptionShortcut(shortcut, optionName)

Adds a new option shortcut which maps the shortcut to a specific optionName.

Kind: instance method of CLI

Param Type
shortcut string
optionName string

clI.removeOptionShortcut(shortcut) ⇒ void

Removes an option shortcut.

Kind: instance method of CLI

Param Type
shortcut string

clI.parseOptions(command, initialOptions) ⇒ Object

Responsible for parsing the command line arguments and determining what is an option and what is an argument.

Generally there is no reason to overload this.

Kind: instance method of CLI

Param Type Description
command Array line arguments.
initialOptions Object

CLIUtils

A selection of utilities to speed CLI development.

Kind: global class


cliUtils.resolve(filename) ⇒ string

Given some filename, resolve that filename relative to your current working directory, or if that fails, against the directory of the calling module.

Kind: instance method of CLIUtils

Param Type
filename string

cliUtils.readFile(filename, [encoding]) ⇒ string

Given some filename, read the contents of that file from the file system and return it as a string.

Kind: instance method of CLIUtils

Param Type Default
filename string
[encoding] string "&quot;utf-8&quot;"

cliUtils.readSTDIN([encoding]) ⇒ string

If your CLI had data piped to it, this command will read that data from the stdin pipe and return it as a string. Subsequent calls to this function will return a cached copy of the stdin data.

Note, that this command is not intended to be used to read user input from stdin. Please use node.js' readline library.

Kind: instance method of CLIUtils

Param Type Default
[encoding] string "&quot;utf-8&quot;"

CommandCLI ⇐ AbstractCommand

THe root command for a command based CLI that uses sub-commands.

Kind: global class
Extends: AbstractCommand


new CommandCLI()

Constructs a new CLI.


commandCLI.title ⇒ string

Returns the title of this root CLI. Should be overloaded by extending classes.

Kind: instance property of CommandCLI
Overrides: title


commandCLI.description ⇒ string

Returns the description of this root CLI. Should be overloaded by extending classes.

Kind: instance property of CommandCLI
Overrides: description


commandCLI.usage ⇒ string

Returns the usage of this root CLI. Should be overloaded by sxtending classes.

Kind: instance property of CommandCLI
Overrides: usage


commandCLI.help() ⇒ void

The default help implementation for a AbstractCommand. Overload this if you want to customize your help display.

Kind: instance method of CommandCLI


commandCLI.addCommand(name, command, description)

Add a new sub-command to this command.

name the name of the command, as it would be used.

command the command itself, which can be the following:

  • function commands, are simple functions that get passed the signature (args,options) when the command is executed. This approach is great for very simple "command CLIs".

  • filenane commands will, if the file exists and exports a sub-class of the AwesomeCLI.AbstractCommand class, load it as javascript, and create an instance of it. This lets you write sub-commands in their own class space and easily load them into AwesomeCLI. This is the best approach to doing commands and highly recommended.

    • AbstractCommmand commands, are similar to filename commands, but they dont do the loading as you are already providing the loaded instance to AwesomeCLI.

description Help descriptive text for this command.

Kind: instance method of CommandCLI

Param Type
name string
command function | AbstractCommand | string
description string

commandCLI.removeCommand(name) ⇒ void

Remove a command by name.

Kind: instance method of CommandCLI

Param Type
name string

commandCLI.getCommand(name) ⇒ Object

Get the command object for a given command name.

Kind: instance method of CommandCLI

Param Type
name string

commandCLI.getCommands() ⇒ Array.<string>

Returns an array of all command names for this particular command. Does not return commands defined by ancestor commands.

Kind: instance method of CommandCLI


commandCLI.run(args, options) ⇒ Promise

Executes the given command. Generally speaking you should not overload this function but instead overload execute(args,options) or before(args,options) or after(args,options).

Kind: instance method of CommandCLI

Param Type
args Array
options Object

commandCLI.init(command, parsed) ⇒ Object

Called by the run() method to initiate the root command and option parsing. Generally this doesn't need to be overloaded.

Kind: instance method of CommandCLI
Returns: Object - the parsed arguments and options.

Param Type Description
command Array line arguments.
parsed Object options and their values.

commandCLI.getOption(optionName) ⇒ Object

Returns the object used to describe an option after it has been added.

Kind: instance method of CommandCLI

Param Type
optionName string

commandCLI.getOptions() ⇒ Array.<string>

Returns an array of all the option names that have been added.

Kind: instance method of CommandCLI


commandCLI.addOption(optionName, [type], [defaultValue], [description])

Adds a new options.

optionName must be unique to this command and any descendant commands.

type can be "*", "boolean", "number", or "string".

defaultvalue is the value used if the option is not prvided.

description is the help description for this option.

Kind: instance method of CommandCLI

Param Type Default
optionName string
[type] string "&quot;*&quot;"
[defaultValue] *
[description] string "&quot;&quot;"

commandCLI.removeOption(optionName) ⇒ void

Removes a given option.

Kind: instance method of CommandCLI

Param Type
optionName string

commandCLI.getOptionShortcut(shortcut) ⇒ string

Returns the Shortcut object for a given shortcut.

Kind: instance method of CommandCLI

Param Type
shortcut string

commandCLI.getOptionShortcuts() ⇒ Array.<string>

Returns an array of all the shortcut names added.

Kind: instance method of CommandCLI


commandCLI.addOptionShortcut(shortcut, optionName)

Adds a new option shortcut which maps the shortcut to a specific optionName.

Kind: instance method of CommandCLI

Param Type
shortcut string
optionName string

commandCLI.removeOptionShortcut(shortcut) ⇒ void

Removes an option shortcut.

Kind: instance method of CommandCLI

Param Type
shortcut string

commandCLI.parseOptions(command, initialOptions) ⇒ Object

Responsible for parsing the command line arguments and determining what is an option and what is an argument.

Generally there is no reason to overload this.

Kind: instance method of CommandCLI

Param Type Description
command Array line arguments.
initialOptions Object

InvalidCommandError ⇐ Error

Thrown if a command that is being added is not a Function, AbstractCommand, or filename string.

Kind: global class
Extends: Error


InvalidOptionError ⇐ Error

Thrown when an option is used when executing the CLI but that option does not exist or is otherwise not allowed.

Kind: global class
Extends: Error


InvalidOptionValueError ⇐ Error

Thrown when a user of the CLI passes in a value to the CLI that does not match the given type for the option, for example passing a string when a number is required.

Kind: global class
Extends: Error


InvalidTypeError ⇐ Error

Thrown when an option is being added but the given type is not "*" or "string" or "number" or "boolean".

Kind: global class
Extends: Error


MissingCommandError ⇐ Error

Thrown when a command CLI gets a command it does not have a command mapped to.

Kind: global class
Extends: Error


MissingOptionValueError ⇐ Error

Thrown when a user of the CLI gives a specific option but does not follow it with a required argument.

Kind: global class
Extends: Error


AbstractCLI

Defines the AwesomeCLI main object, whcih returns pointer to the various AwesomeCLI classes.

Kind: global constant


new AbstractCLI()

Constructs a new CLI.


abstractCLI.title ⇒ string

Returns the title of this root CLI. Should be overloaded by extending classes.

Kind: instance property of AbstractCLI


abstractCLI.description ⇒ string

Returns the description of this root CLI. Should be overloaded by extending classes.

Kind: instance property of AbstractCLI


abstractCLI.usage ⇒ string

Returns the usage of this root CLI. Should be overloaded by sxtending classes.

Kind: instance property of AbstractCLI


abstractCLI.help() ⇒ void

The default help implementation for a root CLI. Overload this if you want to customize your help display.

Kind: instance method of AbstractCLI


abstractCLI.init(command, parsed) ⇒ Object

Called by the run() method to initiate the root command and option parsing. Generally this doesn't need to be overloaded.

Kind: instance method of AbstractCLI
Returns: Object - the parsed arguments and options.

Param Type Description
command Array line arguments.
parsed Object options and their values.

abstractCLI.run(command, parsed) ⇒ Promise

Called when you start your CLI, this takes care of initializing, parsing arguments, and ensuring everything is run smoothly.

By and large there is zero reason to overload this. You should overload execute(args,options), before(args,options), or after(args,options) instead.

Kind: instance method of AbstractCLI

Param Type Description
command Array line arguments.
parsed Object options and their values.

abstractCLI.getOption(optionName) ⇒ Object

Returns the object used to describe an option after it has been added.

Kind: instance method of AbstractCLI

Param Type
optionName string

abstractCLI.getOptions() ⇒ Array.<string>

Returns an array of all the option names that have been added.

Kind: instance method of AbstractCLI


abstractCLI.addOption(optionName, [type], [defaultValue], [description])

Adds a new options.

optionName must be unique to this command and any descendant commands.

type can be "*", "boolean", "number", or "string".

defaultvalue is the value used if the option is not prvided.

description is the help description for this option.

Kind: instance method of AbstractCLI

Param Type Default
optionName string
[type] string "&quot;*&quot;"
[defaultValue] *
[description] string "&quot;&quot;"

abstractCLI.removeOption(optionName) ⇒ void

Removes a given option.

Kind: instance method of AbstractCLI

Param Type
optionName string

abstractCLI.getOptionShortcut(shortcut) ⇒ string

Returns the Shortcut object for a given shortcut.

Kind: instance method of AbstractCLI

Param Type
shortcut string

abstractCLI.getOptionShortcuts() ⇒ Array.<string>

Returns an array of all the shortcut names added.

Kind: instance method of AbstractCLI


abstractCLI.addOptionShortcut(shortcut, optionName)

Adds a new option shortcut which maps the shortcut to a specific optionName.

Kind: instance method of AbstractCLI

Param Type
shortcut string
optionName string

abstractCLI.removeOptionShortcut(shortcut) ⇒ void

Removes an option shortcut.

Kind: instance method of AbstractCLI

Param Type
shortcut string

abstractCLI.parseOptions(command, initialOptions) ⇒ Object

Responsible for parsing the command line arguments and determining what is an option and what is an argument.

Generally there is no reason to overload this.

Kind: instance method of AbstractCLI

Param Type Description
command Array line arguments.
initialOptions Object