Skip to content
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

Closed
wants to merge 6 commits into from

Conversation

itsspriyansh
Copy link
Contributor

@itsspriyansh itsspriyansh commented Jul 6, 2024

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.

image

Throws error for unknown boolean option

image

Throws error for unknown valued option

image

If more than one main option is present then we throw an error.

image

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.

image

Any valued option meant to be used with a specific main option will throw error if used with any other main option.

image

Everything works fine even if no value is passed along with the valued option.

image

Copy link
Member

@garg3133 garg3133 left a 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.

@itsspriyansh
Copy link
Contributor Author

@garg3133 I have updated the PR and the image.

@itsspriyansh itsspriyansh mentioned this pull request Jul 17, 2024
@itsspriyansh
Copy link
Contributor Author

itsspriyansh commented Jul 19, 2024

@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;
}

valuedOptions key is present twice in the Subcommand interface because there are two cases to handle.

# 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);
Copy link
Member

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?

Copy link
Contributor Author

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).

@itsspriyansh
Copy link
Contributor Author

@garg3133 PR updated.

Comment on lines +109 to +114
} 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;
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Contributor Author

@itsspriyansh itsspriyansh Aug 17, 2024

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.

@garg3133
Copy link
Member

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.

@garg3133
Copy link
Member

Completed in #64.

@garg3133 garg3133 closed this Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants