-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds cli argument module (#155)
* ci(linter): adds Node globals to linters Prevents things like `process` from being flagged as undeclared. * feat: new module for reading cli arguments - reads arguments e.g. `command arg1 arg2` - reads named arguments .e.g `command --foo=bar --baz flower' * fix: corrects linter issues These were due to adding the node globals to the linter configuration
- Loading branch information
1 parent
dcf5a70
commit b7943b6
Showing
9 changed files
with
87 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
|
||
const ado = require("./src/ado"); | ||
const AzureStorage = require("./src/azure") | ||
const cliArguments = require('./src/cli-arguments.js'); | ||
const config = require('./src/config.js') | ||
const credentials = require("./src/credentials.js"); | ||
const database = require("./src/database"); | ||
const helpers = require("./src/helpers"); | ||
const jira = require("./src/jira"); | ||
|
||
module.exports = { | ||
ado, | ||
AzureStorage, | ||
cliArguments, | ||
config, | ||
credentials, | ||
database, | ||
helpers, | ||
jira, | ||
ado | ||
jira | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Parses command-line parameters and returns an array of the parameters. This is really getting the | ||
* "arguments" passed from the command-line, but there is no additional parsing or formatting, so | ||
* this is best suited to just parameters. You could handle each element in the array yourself for | ||
* involved processing | ||
* | ||
* `node script.js beau 42` | ||
* | ||
* returns | ||
* `[ 'beau', '42' ]` | ||
* | ||
* * | ||
* @returns Array | ||
*/ | ||
const getParameters = () => { | ||
// process.argv is an array where: | ||
// - The first element is the path to the Node.js executable (e.g., '/usr/local/bin/node') | ||
// - The second element is the path to the script file (e.g., '/path/to/script.js') | ||
// - The subsequent elements are the command-line arguments | ||
|
||
// Get the arguments starting from the third element | ||
const args = process.argv.slice(2).length > 0 ? process.argv.slice(2) : [] | ||
return args | ||
} | ||
|
||
/** | ||
* Parses named command-line arguments. This only supports long-named arguments prefixed with a `--`. | ||
* Short arguments .e.g. `-d` are not supported. The value can either be space separated or with an | ||
* equals sign | ||
* | ||
* `node name-arguments.js --age=42 --name=Beau` | ||
* | ||
* Using this function this will return | ||
* | ||
* { age: '42', name: 'Beau' } | ||
* | ||
* | ||
* @returns Object | ||
*/ | ||
const getNamedArguments = () => { | ||
const args = {}; | ||
|
||
const argv =process.argv; | ||
// Start from index 2 to skip the node executable and script file paths | ||
for (let i = 2; i < argv.length; i++) { | ||
let arg = argv[i]; | ||
|
||
if (arg.startsWith('--')) { | ||
// Remove the leading -- | ||
arg = arg.slice(2); | ||
|
||
// Handle --key=value | ||
if (arg.includes('=')) { | ||
const [key, value] = arg.split('='); | ||
args[key] = value; | ||
} else { | ||
// Handle --key value | ||
const key = arg; | ||
const value = argv[i + 1]; | ||
|
||
if (!value || value.startsWith('--')) { | ||
args[key] = true; // For flags without values | ||
} else { | ||
args[key] = value; | ||
i++; // Skip the next item as it's the value for this key | ||
} | ||
} | ||
} | ||
} | ||
|
||
return args; | ||
} | ||
|
||
|
||
module.exports = { | ||
getNamedArguments, | ||
getParameters | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters