-
Notifications
You must be signed in to change notification settings - Fork 15
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
verify options for subcommands #51
Conversation
b61d94d
to
6b0feb8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of --help
flag with a subcommand, instead of going inside the respective subcommand file and then calling verifyOptions
from there, we should probably call the showHelp
function from the first line of the subcommands/index.ts > run()
method itself. We don't need to do any checks (like check for Java, ANDROID_HOME
env, etc.) if the user just want to get the --help
for the subcommand.
Also, leave a new line at the end of the help section.
@garg3133 I have updated the PR and the image. |
@garg3133 I have updated the PR. Please check the updated PR description to see the example cases. This is going to be the new interface for available subcommands: export interface ValuedOptions {
name: string,
alias: string[],
description: string,
}
export interface Subcommand {
description: string;
valuedOptions?: ValuedOptions[];
options: {
name: string;
description: string;
valuedOptions: ValuedOptions[];
}[];
}
export interface AvailableSubcommands {
[key: string]: Subcommand;
}
# If valued option is passed for a subcommand
npx @nightwatch/mobile-helper android disconnect --deviceId <device_id>
# if valued option is passed for an option of a subcommand
npx @nightwatch/mobile-helper android connect --emulator --avd <avd_name> |
const optionsPassed = Object.keys(options).filter(option => options[option] !== false); | ||
const availableOptions = AVAILABLE_SUBCOMMANDS[subcommand].options; | ||
|
||
const availableOptionsNames = availableOptions.map(option => option.name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're only considering the main name of the options here, wouldn't it cause the corresponding aliases to be moved to the valuedOptionsPassed
instead of the mainOptionsPassed
, which would result in unknown valued option passed error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that should be an issue here since we haven't decided to use any aliases for the subcommand options. Aliases exists only for the flags (valued options).
@garg3133 PR updated. |
} else if (mainOptionsPassed.length === 0 && optionFlagsPassed.length) { | ||
// If the main option is not present, then any other options present are invalid. | ||
Logger.log(`${colors.red(`Unknown option(s) passed for subcommand ${subcommand}:`)} ${optionFlagsPassed.join(', ')}`); | ||
showHelp(subcommand); | ||
|
||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just asking, don't make any changes: wouldn't the above block misbehave for subcommands like disconnect
which does not accept any main option but only the optionFlags?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function wasn't intended to be used for subcommands like disconnect. Since there is just one workflow for disconnect and no main option, so the disconnect/index.ts
file itself is used for the disconnect script. Unlike other scripts where the index.ts
file is used to verify options and facilitate default flow. In this case, there is just one thing we have to verify, that is, if there is any other option other than the valued flags then we will show error otherwise everything works fine.
So shall we just manually check this before calling the disconnect script (which I originally considered), or shall we made a minor addition to the verifyOptions
to do this instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, updating verifyOptions
appears a better idea to me.
Since there were a lot of things going on in this PR and it was difficult to wrap my head around it all (mostly due to the confusing naming scheme and use of "options" at contradicting places), I tried to simplify it with #64. Please review it once. |
Completed in #64. |
Description
This PR adds the functions for verifying the option passed with subcommands.
verifyOptions
checks if the option is valid or not.showHelp
shows usage instructions for a specific subcommand.Moves the repetitive logic of displaying subcommand's options help to a separate function.
Examples
Shows help for individual subcommand without having to check environment variables.
Throws error for unknown boolean option
Throws error for unknown valued option
If more than one main option is present then we throw an error.
If more than one main option is present then we throw the same error irrespective of any other option present. This is because we need the main option first before we can verify any other options by going one level inside.
Any valued option meant to be used with a specific main option will throw error if used with any other main option.
Everything works fine even if no value is passed along with the valued option.