-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic support for arguments #127
Comments
Hey @G-Rath, can you give me a use case? I don't quite get it why we should add this feature. |
I have a small project that 'manages' a JSON schema; mainly getting data from a source and validating it against that schema. As part of this, it runs 'migrations' to aid in making older outdated data compatible with newer versions of the schema before running it's validation. As a result, to make things easier, I've created a 'create-migration' script: /**
* Script for creating a new migration file for the `DataMigrator`,
* using `AbstractDataMigration`, in `./src/migrators/migrations/`.
*/
// ensure that process.env.INIT_CWD is set to *something*
require('./functions/ensureInitCwdEnvironmentValue')(__dirname);
const /** @type {module:path} */ path = require('path');
const /** @type {module:fs} */ fs = require('fs');
const args = require('args');
const flags = args
.option(
'message',
'The deprecation warning message',
''
)
.option(
'datetime',
'The datetime this migration was created on',
(new Date()).toISOString()
)
.option(
'force',
'Forcibly create the migration, even if it already exists',
false
)
.parse(process.argv, { value: '<className>' });
const [className] = args.sub; // the first property should be the name
if (!className) {
console.error('ERROR: the name property is required');
process.exit(1);
}
// ... make the migration, write to file, etc etc This script requires the name of the migration class as an argument, and has the following help output:
Now, I can add I also have to do
To make These two points are what Ultimately, I'd love to be able to do: const flags = args
.argument(
'className',
`The name of the migration class. Since this will also be used as the filename for the class, it shouldn't contain any file extensions, or invalid symbols.`
)
.option(
'message',
'The deprecation warning message',
''
)
.option(
'datetime',
'The datetime this migration was created on',
(new Date()).toISOString()
)
.option(
'force',
'Forcibly create the migration, even if it already exists',
false
)
.parse(process.argv); which would result in something like this:
|
Given #13 &
args.sub
, I think it's a reasonable next step for this library to provide some basic support for arguments.I think it's relatively easy to do, given that once you're done parsing the options,
args.sub
gives you what's left. You could then do a new parse on that for arguments that are specified via anargument
method, that takes similar parameters to theoption
method.The tricky bit is in the return. For now I'd say just keep it as is; since we can get the values out of
args.sub
, and they're order dependent, I think it's reasonable to add this feature initially just for exiting if arguments are missing, and including arguments nicely in the help menu.Following that, I'd consider adding an
arguments
property containing a map of the arguments, just to make it nicer to access using destructuring in a manner that's less order-dependent.I'm happy & willing to take a crack at adding this myself over the next couple of days, if this is something you'd be happy to have added to the library.
Pointless code example:
The text was updated successfully, but these errors were encountered: