I have the following code on my app:
import commander = require('commander');
commander
.option('-u, --user [user]', 'user code')
.option('-p, --pass [pass]', 'pass code')
.parse(process.argv);
Then I try to access:
commander.user
But I get an error (commander.d.ts from DefinitelyTyped):
user does not exist on type IExportedCommand
I tried adding this
interface IExportedCommand {
user: string;
pass: string;
}
But I still get the error. How can I fix this?
You write code to describe your command line interface. Commander looks after parsing the arguments into options and command-arguments, displays usage errors for problems, and implements a help system.
You can run typescript directly on the node with the ts-node package. This package is recommended for development only. To make the final deploy in production, always use the javascript version of your project. The ts-node is already included as a dependency on another package, t ts-node-dev .
In a terminal window, navigate to the source code folder you created. Then, run the following command: npm run compile-typescript Now, you should now see a new folder created named output, that contains one file named script.js. Notice how the output has defaulted to ES5 JavaScript, which is compatible with all major browsers.
You can use any of these scripts in npm: Prepare will run before the package is packed and published, and also after it is installed. prepublish Only will run before preparation and only when you publish npm. Perversion will run before you upgrade to a new package version.
To save time the "compile-typescript" and "start" commands can be combined into one command by modifying the start command to include this functionality. Now running the command npm run start will first run the "compile-typescript" command and then use node to run the script.js file that is output.
The script.js created as a result of running the "compile-typescript" command can now be run with Node.js. To do this another package.json script is added, which is named "start". The "start" script will run the node cli command which the path of the script.ts file is passed.
You can also do:
npm install --save-dev @types/commander
EDIT: Commander now includes its own TypeScript typings, so this should no longer be necessary.
Create a file commander-expansion.d.ts
with the following :
declare namespace commander {
interface IExportedCommand extends ICommand {
user: string;
pass: string;
}
}
Because I did something like this recently, recommend --auth user:password
. Saves you dealing with user missing username or password. But prevents using :
as a password property
¯\_(ツ)_/¯
More : https://github.com/alm-tools/alm/blob/master/src/server/commandLine.ts#L24
You can keep the Typescript interface within the same file.
interface InterfaceCLI extends commander.ICommand {
user?: string
password?: string
}
After you can define this interface to a variable after running the program.parse
function.
const cli: InterfaceCLI = program.parse(process.argv)
console.log(cli.user, cli.password)
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With