The command-line arguments are easily accessible in KV form, like this: args.a args. When the above code is executed, we get the following output: > bun run command_line_yargs.js -a 1 -b 2 -c -d e f g Unparsed args: Parsed args: Next, here is the code that uses yargs-parser to parse the command-line arguments: const yargs = require("yargs-parser") const rawArgs = (3) const args = yargs(rawArgs) console.log("Unparsed args:", rawArgs) console.log("Parsed args:", args) Second, yargs throws an exception when executed under bunįirst, we need to install the yargs-parser package: > bun install yargs-parser bun add v0.1.13 installed done.First, I’m interested only in parsing the args, nothing else. ![]() I’ve used yargs-parser instead of yargs because of two reasons: ![]() Step 2: Create an app.js file in the particular directory. As Bun natively supports all the NPM modules, we can simply use the popular yargs-parser package to parse the args and give them back in an easy-to-use structure. Steps to print command-line arguments: Step 1: Install Node.js if Node.js is not installed on your machine. Using yargs-parserĪs we’ve just seen, the native support is pretty okay. That’s where NPM module yargs-parser comes into the picture. That problem has been solved a long time back. But, in the real world, who would be interested in parsing the command line args. The native support for command-line args is pretty okay. We’ll get the following output: All args: Program: command_line_args.js Arg 3 onwards: If we run app.js with the following command: > bun run command_line_args.js -a 1 -b 2 -c -d e f g Have a look at the following: // app.js console.log('All args:', Bun.argv) console.log("Program:", Bun.argv) console.log("Arg 3 onwards:", (3)) The program name can also be ignored in most of the cases. The first two elements in Bun.argv are of no use, so should be ignored. In the above case, the Bun.argv will contain everything from start to end: // Have a look at this: > bun run someProg.js a b By complete, I mean that ‘bun’ and ‘run’ are also present in the Bun.argv array. Just like Node.js, Bun has an array of strings called Bun.argv that contains the space separated list of the complete command (not only arguments). In this article, we’ll learn how to get command-line arguments in Bun natively and through an NPM module, yargs-parser. Sometimes even the config file path gets passed through the command-line argument.īeing a runtime, of course, Bun supports the command-line arguments supplied to the application. How to get command-line arguments in Bun?Įvery application needs to handle command-line arguments, unless all the options are read from a config file. License : (ISC ) About to write to D :\nodejs\file -example\ package. ![]() package name : (file -example )ĭescription : Nodejs read argument from command line example Use `npm install ` afterwards to install a package and See `npm help init` for definitive documentation on these fields ![]() It only covers the most common items, and tries to guess sensible defaults. This utility will walk you through creating a package.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |