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

Enhance help text generation #112

Open
S4G4R opened this issue Jan 28, 2025 · 5 comments
Open

Enhance help text generation #112

S4G4R opened this issue Jan 28, 2025 · 5 comments

Comments

@S4G4R
Copy link

S4G4R commented Jan 28, 2025

Printing help text is currently facilitated using functions such as format-opts and format-table, which might suffice for simple applications. But it quickly gets complicated when we have layers of nested commands and subcommands which have their own opts and args.

Given a spec and args, we should be able to generate help text using a single function call.

Additionally, we could consider including --help or -h as implicit opts for every command and subcommand. Running a command with missing opts and/or args could result in automatically printing help text. cli-matic handles this well.

@devurandom
Copy link

Help for subcommands could e.g. work like this:

$ example
Commands:
cmd1  First command
cmd2  Second command

Use "example help <command>" for more information about a given command.
Use "example options" for a list of global command-line options (applies to all commands).


$ example options
The following options can be passed to any command:
  --foo:
      Description
      spanning multiple lines.


$ example help cmd1
Short description

More details
on multiple lines.

Usage:
  example cmd1 [options] <bar> [baz]

Options:
  bar:
      Required argument description.
  baz:
      Optional argument description.
  --bag='default':
      Option description
      spanning multiple lines.


$ example help cmd2
Description.

Usage:
  example cmd2 [options] [foo]

Options:
  foo:
      Optional argument description.
  --fog=[]:
      Description.

@borkdude
Copy link
Contributor

Using the following function:

(defn format-commands [{:keys [table]}]
  (let [table (mapv (fn [{:keys [cmds desc]}]
                      (cond-> [(str/join " " cmds)]
                        desc (conj desc)))
                    (filter (comp seq :cmds) table))]
    (cli/format-table {:rows table})))

we can produce, from e.g. this table:

(def table
  [{:cmds ["copy"]   :fn copy   :args->opts [:file] :desc "Copy a file"}
   {:cmds ["delete"] :fn delete :args->opts [:file] :desc "Delete a file"}
   {:cmds []         :fn help}])
  copy   Copy a file
  delete Delete a file

@borkdude
Copy link
Contributor

Sorry I wasn't done, but I hit "Comment" accidentally.

The options can already be formatted using format-opts. Honestly the rest of the proposition is too opiniated in my opinion.
So it seems to be that just adding the function format-commands (or perhaps format-table is a better name) would help a bit?

@borkdude
Copy link
Contributor

@devurandom
Copy link

So it seems to be that just adding the function format-commands (or perhaps format-table is a better name) would help a bit?

Sounds great and is probably good enough for most. Thanks for considering this topic!

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

No branches or pull requests

3 participants