Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR reshapes the help and usage-error API around the default path most programs should use: define commands, call
ParseAndRun, and get sane help output without wiring anything else.The core API stays string-based and automatic. The composable usage/help builder is kept internal in this PR while that API continues to be refined.
Default help
That gets help for free:
Flags
Flags still use the standard library
flagpackage.FlagConfigsadds the small bits this library owns: short aliases, required flags, and local flags.Help shows both flag forms, marks required flags, and wraps long descriptions:
If setup inside
FlagsFuncis invalid, such as defining the same flag twice,Parsereturns a normal setup error instead of crashing with a panic stack.Summary and Description
For commands that need both parent-list text and longer command help, use
SummaryandDescriptiontogether:Parent help uses the short text:
todo list --helpuses the longer description:If a command only needs one sentence, set
Summaryand leaveDescriptionempty.Custom help
Most commands should not set
Help. When a command needs full control,Helpreplaces the generated help string:Usage errors
When parsing succeeded but
Execfinds invalid args or flag combinations, returnUsageErrorf.Runprints the resolved command's help to stderr, then returns the underlying error so callers can print their normalerror: ...line.Return a normal error when help should not be printed.
State.Cmdis the resolved command, so command code can inspects.Cmd.Path()when it needs command-aware errors or logging.Split parse/run
ParseAndRunremains the normal entry point and handles--helpautomatically. Programs that callParsedirectly should check the standard sentinel:Breaking changes
Command.UsageFunc; useCommand.Helpwhen replacing help entirely.Command.ShortHelpwithCommand.Summaryfor command lists andCommand.Descriptionfor longer command help.FlagOptiontoFlagConfigandCommand.FlagOptionstoCommand.FlagConfigs.DefaultUsageand the top-levelUsagefunction from the public API.cli.ErrHelp; checkerrors.Is(err, flag.ErrHelp)when handlingParsedirectly.Why: the library should provide good default help without making usage rendering the center of the API. Custom help remains possible, usage errors become explicit, and the structured help-building API can keep evolving outside the public surface.
Closes #4, #14, #15.