-
-
Notifications
You must be signed in to change notification settings - Fork 151
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
Generate help by flag descriptions #140
Conversation
I don't think |
Also look at how https://github.com/yargs/yargs solves this and how it presents the output. Also do some research on how other arguments parsers in Python and Ruby handles the config and output. |
There should probably also be a method to get the autogenerated help text in case you want to manually combine it with your own help text. |
Sorry for my late reply. I will work on your review this weekend. |
I looked at some command-line helpers and some command-line tools. For example:
As a result, I think the following format is better:
What do you think? |
I agree with everything except the shortflag order. The shortflag should be last. |
Do any of the libraries put more than one space before |
Another good library for inspiration: https://github.com/apple/swift-argument-parser |
OK, I'll do it. 👍
I have no strong opinion about it. How many spaces are appropriate? |
2 |
Sorry for the late response... I've addressed all the review points except for the following one:
About the comment above, what kind of API do you expect? |
generate-help.js
Outdated
return lines; | ||
} | ||
|
||
module.exports = function ({description, help, flags}, pkg) { |
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.
module.exports = function ({description, help, flags}, pkg) { | |
module.exports = ({description, help, flags}, pkg) => { |
generate-help.js
Outdated
|
||
lines = lines.map(line => trimNewlines(line)); | ||
|
||
const content = lines.join(EOL).replace(/^\t+/gm, '').replace(/[\t ]+[\r\n]*$/gm, ''); |
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.
Instead of the regexes here, couldn't you use redent
?
generate-help.js
Outdated
|
||
const maxNameLengh = Math.max(...entries.map(({name}) => name.length)); | ||
|
||
const lines = entries.map(({name, desc}) => { |
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.
Don't use abbreviations for variable/parameter names.
generate-help.js
Outdated
lines = lines.map(line => trimNewlines(line)); | ||
|
||
const content = lines.join(EOL).replace(/^\t+/gm, '').replace(/[\t ]+[\r\n]*$/gm, ''); | ||
return EOL + trimNewlines(redent(content, 2)) + EOL; |
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.
Use \n
instead of EOL
Any suggestions? |
For example: const { generateHelp } = require("meow");
generateHelp({ help: "...", description: "...", flags: {...} }, packageJson); |
Edited: const { generateHelp } = require("meow");
generateHelp({ help: "...", description: "...", defaultDescription: "...", flags: {...} }); |
Thinking more about it, maybe instead of a free method, we could make the |
Thanks for your advice.
It seems a better API than my suggestion! 👍 const cli = meow({
help: (wholeText: string, flagsSection: string, description: string, options: meow.Options) => {
return `...`;
},
flags: {
input: {
type: 'string',
description: 'Input file path'
}
}
}); |
Yes, but I think it’s better to make |
Make sense. I'll try it, thanks! |
The |
I'm ready to be reviewed again. |
index.d.ts
Outdated
/** | ||
Callback function to generate a help text you want. | ||
*/ | ||
type GenerateHelp = (args: Readonly<{wholeText: string; flagLines: readonly string[]; description: string; options: Readonly<Options<AnyFlags>>}>) => string; |
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.
The args
type should be a separate interface
and each property should have a doc-comment.
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.
args
should have a better name too.
index.d.ts
Outdated
@@ -27,6 +28,11 @@ declare namespace meow { | |||
|
|||
type AnyFlags = {[key: string]: StringFlag | BooleanFlag | NumberFlag}; | |||
|
|||
/** | |||
Callback function to generate a help text you want. |
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 needs to be more verbose. Document the default behavior, etc.
Some things to keep in mind:
|
Bump |
Sorry for the late response.
I have no good ideas about the points above. Could you please help me? |
@@ -0,0 +1,103 @@ | |||
'use strict'; |
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 file needs to be added to files
in package.json.
function buildFlagLines(flags) { | ||
flags = {...flags, help: {type: 'boolean', description: 'Show help'}}; | ||
|
||
const entries = Object.entries(decamelizeKeys(flags, '-')).map(([name, def]) => { |
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.
Don't use abbreviations (regarding def
, and other cases).
} | ||
|
||
/** | ||
Callback function to customize a help text you want. |
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.
Typos
|
||
The input is reindented and starting/ending newlines are trimmed which means you can use a [template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) without having to care about using the correct amount of indent. | ||
|
||
The description will be shown above your help text automatically. | ||
|
||
Also, you can customize the auto-generated help text by giving the function as follows: |
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.
The readme and index.d.ts should be in sync.
The code is quite hard to follow. Some code comments could maybe help that too. I often comment "simplify" on PRs as honestly most PRs could be simplified. Nobody writes perfect code the first time around. It helps to come back to it and look it over and try to simplify/refactor it. That's what I'm asking.
I'm just asking you to look over and try to add some more tests. I'm the one that will be stuck maintaining this for infinity, so I want to ensure it's as solid as possible. |
@@ -28,6 +29,37 @@ declare namespace meow { | |||
type AnyFlag = StringFlag | BooleanFlag | NumberFlag; | |||
type AnyFlags = {[key: string]: AnyFlag}; | |||
|
|||
interface GenerateHelpOptions { | |||
/** | |||
A whole help text generated by default. |
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.
Typo
description: string; | ||
|
||
/** | ||
A list of a line including each flag's name and description. |
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.
Typo
flagLines: readonly string[]; | ||
|
||
/** | ||
An object including each flag information. |
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.
Not clear to the user what "flag information" means.
This change needs more docs. There's not even a mention of the new behavior of generating flag docs by default. |
as for yargs, this is not fully true as they print in with a little better indentations example:
|
I am very sorry for my too late response. I can no longer go any further with this PR due to my lack, in spite of many advice I have received. 🙇 I am so grateful for the kind review. Thank you. |
This fixes #80. Rechallenge of PR #82.
Example
Welcome any feedback!