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

Add isRequired flag option #141

Merged
merged 26 commits into from May 7, 2020
Merged

Add isRequired flag option #141

merged 26 commits into from May 7, 2020

Conversation

sbencoding
Copy link
Contributor

@sbencoding sbencoding commented Mar 24, 2020

This PR aims to add the ability to specify an isRequired option for flags, based on code and feedback from #110 and #51.

What I did

  • Implement the isRequired option. Can be a boolean or a function.
  • Update readme and type definition with the isRequired option.
  • Add tests and new fixture files for the new feature

Questions/suggestions

I think it's enough to pass the flags and the input variables to the isRequired function. That should give the consuming application enough context to decide the return value, at least from the package's point of view IMO.

In the original issue it was suggested that this should be entirely handled by the consuming application. I didn't know if I should, but it's possible to add an additional flag that changes the behavior of isRequired, so that instead of exiting and printing the missing options, it would include the missing flags in the result returned by the API.

I didn't add any additional tests to index.test-d.ts because I wasn't sure what to test for. isRequired either closes the app if some value is missing or leaves the value untouched if it's specified, so I don't know if the feature needs any type related testing.

Let me know what you think and if I should change anything.

Fixes #51


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


IssueHunt has been backed by the following sponsors. Become a sponsor

index.js Show resolved Hide resolved
readme.md Outdated
@@ -97,6 +97,7 @@ The key is the flag name and the value is an object with any of:
- `type`: Type of value. (Possible values: `string` `boolean` `number`)
- `alias`: Usually used to define a short flag alias.
- `default`: Default value when the flag is not specified.
- `isRequired`: Boolean or Function that specified is this flag is required.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to describe the parameters the function receives.

readme.md Outdated
@@ -106,6 +107,9 @@ flags: {
type: 'string',
alias: 'u',
default: 'rainbow'
isRequired: (flags, input) => {
if (flags.otherFlag) return true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use braces

index.js Outdated
if (missing.length > 0) {
console.log(`Missing required option${missing.length > 1 ? 's' : ''}`);
for (const flag of missing) {
console.log(`\t--${flag.key}${flag.alias ? `, -${flag.alias}` : ''}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be console.error?

index.js Outdated

// Print error message for missing flags
if (missing.length > 0) {
console.log(`Missing required option${missing.length > 1 ? 's' : ''}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be consistent with the word "flag" instead of "option".

index.js Outdated
@@ -118,6 +128,30 @@ const meow = (helpText, options) => {
}
}

// Get a list of missing flags, that are required
const missing = [];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"missing" what? Needs a more descriptive name.

@@ -0,0 +1,26 @@
#!/usr/bin/env node
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of fixtures now. Can you move them into a test/fixtures directory?

test.js Outdated
@@ -80,6 +80,72 @@ test('spawn cli and test input flag', async t => {
t.is(stdout, 'bar');
});

test('spawn cli and test required flag with missing args', async t => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move these new tests into a new test file? And also move all tests into a test directory?

@sindresorhus sindresorhus changed the title Implement isRequired feature as described in #51 and #110 Add isRequired flag option Apr 27, 2020
@sbencoding
Copy link
Contributor Author

I've implemented the requested changes and merged with the latest master as well, let me know if there's anything else to change.

index.d.ts Outdated
@@ -2,11 +2,13 @@ import {PackageJson} from 'type-fest';

declare namespace meow {
type FlagType = 'string' | 'boolean' | 'number';
type IsRequiredPredicate = (flags: AnyFlags, input: string[]) => boolean;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type IsRequiredPredicate = (flags: AnyFlags, input: string[]) => boolean;
type IsRequiredPredicate = (flags: AnyFlags, input: readonly string[]) => boolean;

readme.md Outdated
- `isRequired`: Boolean or Function that specifies if this flag is required.
Two arguments are passed to the function.
The first arguments is the **flags** object, it contains the flags converted to camelCase excluding aliases.
The second arugment is the **input** string array, it contains the non-flag arguments.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

readme.md Outdated
Two arguments are passed to the function.
The first arguments is the **flags** object, it contains the flags converted to camelCase excluding aliases.
The second arugment is the **input** string array, it contains the non-flag arguments.
The function should return a Boolean, true if the flag is requried, otherwise false.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

readme.md Show resolved Hide resolved
index.d.ts Outdated
Two arguments are passed to the function.
The first arguments is the flags object, it contains the flags converted to camelCase excluding aliases.
The second arugment is the input string array, it contains the non-flag arguments.
The function should return a Boolean, true if the flag is requried, otherwise false.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function docs should be at https://github.com/sindresorhus/meow/pull/141/files#diff-b52768974e6bc0faccb7d4b75b162c99R5 and you don't have to mention the types. It's already shown by the actual types. Mention what it does and what problem it solves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was just trying to make sure that the index.d.ts and the readme.md file reflect each other as much as possible, that's why I left the type information there, because although typescript has a better way of documenting type information, in the readme file this is the only way.
Anyways I left the documentation with the type information in the readme file and updated the typescript file with a better documentation

@@ -0,0 +1,72 @@
import test from 'ava';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filename can just be is-required-flag.js

index.js Outdated

const getMissingRequiredFlags = (flags, receivedFlags, input) => {
const missingRequiredFlags = [];
if (typeof flags !== 'undefined') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an early-return here to reduce the nesting.

@sbencoding
Copy link
Contributor Author

I've implemented the requested changes and merged with the current upstream master branch that contains the isMultiple flag.
Because of this merge I've added a few more test to make sure isRequired plays nicely with it.

@sindresorhus sindresorhus merged commit 1eede6a into sindresorhus:master May 7, 2020
@sindresorhus
Copy link
Owner

sindresorhus commented May 7, 2020

Looks good. Thanks :)

@sbencoding sbencoding deleted the feature-flag-is-required branch May 7, 2020 16:07
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.

Support required flags
2 participants