developersite.blogg.se

Dcommander hack
Dcommander hack








The add action will require us to write note data to a file, so we’ll need access to the Node fs (filesystem) module. We know that we’ll need three basic functions: two utility functions for reading and writing to the filesystem, and a function that handles taking a note input from the user and adding it to a collection.

dcommander hack

Let’s stub out the module with the functions that we know we’ll need. The add script will be responsible for receiving an input (the note given by the add command), adding it to a notebook collection, and finally writing the collection to a local file for persistent storage. We’ll start by creating a file, add.js, in the lib directory. With our command created, it’s time to write the logic that handles the add event. Creating and Using Actions Setting Up the Add Module To add more commands to our application, we would simply add another block of methods before the closing parse() method. We’ll pass in the note argument and simply log it for now. The action() method accepts a callback that has the command arguments passed in. Lastly, we’ll specify the functions to be called when this command is run. Next, we provide a description of the command to be displayed when a user accesses the help output using the -help flag. This allows us to call the add command with either add or a. We’re going to set the command to add and set the note argument as required. The brackets indicate a required argument, while the brackets indicated an optional requirement. The command() method takes a String argument with the following syntax: 'command ', where command is the sub-command name followed by arguments. Once we have our command structure in place, we’ll start populating each method with the appropriate arguments, starting with the command() method. /package.json ' ) const program = require ( ' commander ' ) program.

  • The action() method is where we will pass in command arguments to functions that we’d ultimately like to call for each command.Ĭonst pckg = require ( '.
  • The description() method defines a description for the command to be displayed in the help output.
  • The alias() method is used to set an optional alias to the command.
  • The command() method is used to define the phrase used to call the command.
  • The basic structure for this approach uses the following methods:

    dcommander hack

    We’ll start by laying out the structure of a basic command, which chains several methods together on the Commander instance (in this case, program). This approach allows us to define several different commands, each with the potential to have its own options and help output. We’ll use the second approach, using secondary commands with options. Use git-style sub-commands with options.

    dcommander hack

    There are three primary ways to build commands with Commander:










    Dcommander hack