One option would be to add these examples to your custom usage string for your subcommand:
@main
struct MyDesktop: ParsableCommand {
static var configuration = CommandConfiguration(
commandName: "mydesktop",
abstract: "My Desktop Utility",
discussion: "A utility for managing my Desktop app.",
subcommands: [Logs.self, UpdatePort.self, Uninstall.self, Proxy.self]
)
// If you must supply a subcommand, just omit `run` for the `ParsableCommand`.
//
// Needless to say, if there is some default behavior, you’d either provide a
// `defaultCommand` above, or implement `run`, whichever makes sense for your app.
}
extension MyDesktop {
struct Logs: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Export logs for mydesktop Desktop processes.",
usage: """
mydesktop logs --time <time>
Examples:
$ mydesktop logs --time 10h
$ mydesktop logs -t 30m
"""
)
@Option(
name: [.short, .long],
help: ArgumentHelp(
"Time period for logs (e.g. 10h, 30m, 2d)",
)
)
var time: String
mutating func run() {…}
}
struct UpdatePort: ParsableCommand {…}
struct Uninstall: ParsableCommand {…}
struct Proxy: ParsableCommand {…}
}
That produces, for example:
$ ./mydesktop
OVERVIEW: My Desktop Utility
A utility for managing my Desktop app.
USAGE: mydesktop <subcommand>
OPTIONS:
-h, --help Show help information.
SUBCOMMANDS:
logs Export logs for mydesktop Desktop processes.
update-port Change the service port for my Desktop and restart the service.
uninstall Remove the my Desktop app, including associated data, services, CLI, defaults.
proxy Configure proxy settings via CLI.
See 'mydesktop help <subcommand>' for detailed help.
And if you omit --time for the logs subcommand:
$ ./mydesktop logs
Error: Missing expected argument '--time <time>'
Help: -t <time> Time period for logs (e.g. 10h, 30m, 2d)
Usage: mydesktop logs --time <time>
Examples:
$ mydesktop logs --time 10h
$ mydesktop logs -t 30m
See 'mydesktop logs --help' for more information.
Personally, I'd leave these examples to be discovered in “help”, rather than in the “usage” string:
struct Logs: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Export logs for mydesktop Desktop processes."
)
@Option(
name: [.short, .long],
help: ArgumentHelp(
"Time period for logs (e.g. 10h, 30m, 2d)",
discussion: """
EXAMPLES:
mydesktop logs --time 10h
mydesktop logs -t 30d
"""
)
)
var time: String
mutating func run() {…}
}
Yielding:
$ ./mydesktop logs
Error: Missing expected argument '--time <time>'
Help: -t <time> Time period for logs (e.g. 10h, 30m, 2d)
Usage: mydesktop logs --time <time>
See 'mydesktop logs --help' for more information.
$ ./mydesktop logs --help
OVERVIEW: Export logs for mydesktop Desktop processes.
USAGE: mydesktop logs --time <time>
OPTIONS:
-t, --time <time> Time period for logs (e.g. 10h, 30m, 2d)
EXAMPLES:
mydesktop logs --time 10h
mydesktop logs -t 30d
-h, --help Show help information.
Personally, I think a long discussion of various examples for a particular option belongs in the “help” text, not in an error message. But you can obviously do whatever you want.
By the way, I outlined two options:
- Put it in the
Logs “usage” string; and
- Put it in the “discussion” of the
--time option.
But there are other options, too:
- Put the examples in the main “help” text of the
--time option; and
- Put it in the “discussion” of the
CommandConfiguration for your main ParsableCommand (e.g., MyDesktop in my example).
There are lots of alternatives. These all have their own pros and cons, so try them out and see which you prefer.