Customisation of swift argumentparser's help output and error output

Hello I want to implement customisation to swift argumentparser, Here are following changes want to do it in my cli

  1. changing default footer present in help command output

currently help command output coming like this

OVERVIEW: clisample
USAGE: clisample <subcommand>
 
OPTIONS:
  --version               show the version.
  -h, --help              show the help.
 
SUBCOMMANDS:
  logs (default)          Export logs for clisample processes.
 
See 'clisample --help' for more information.'`

so instead of See 'clisample --help' for more information.' I want For more details, run 'clisample help <subcommand>'

  1. customise error string getting from validation error

currently output of error

Error: Missing value for '-t <time>
Help:  -t <time>  Time window (e.g. 10h, 30m, 2d).
Usage: clisample logs --time <time>
 
  See 'clisample logs --help' for more information.

so I want error output with example and customised footer, like this

Error: Missing value for '-t <time>'
	Help:  -t <time>  Time window (e.g. 10h, 30m, 2d).
	Usage: clisample logs --time <time> 
	Example: clisample logs -t 5m
	 
	  For more details, run 'clisample help <subcommand>'

If this feature is already available, please guide me, as i haven't seen anything on forums and any other place

That level of customization isn't currently present in ArgumentParser. To support that we would probably need some kind of templating system, which isn't part of the library.

That said, you can intercept all the help and error output if you implement your own @main type. The static func main() implementation is fairly simple (you can copy from here) and you could post-process the error text by passing any errors you catch to your command's fullMessage(for:) method:

https://swiftpackageindex.com/apple/swift-argument-parser/1.6.1/documentation/argumentparser/parsablearguments/fullmessage(for:columns:)

1 Like

Some alternatives have been suggested in response to your prior question.

I know this way to customise description of version and help present in help command is below, so by default it is like this

OPTIONS:
--version show the version.
-h, --help show the help.

struct CliSample: ParsableCommand {

**static** **let** configuration = CommandConfiguration(

abstract: "CliSample",
subcommands: [command1.self, command2.self, command3.self, command4.self]
defaultSubcommand: command1.self,
helpNames:

)

@FlagFlag(name: .shortandlong, help: “customised help string“)
var help: Bool = fal@Flage

@Flag(name: .shortandlong, help: “customised version string“)
var version: Bool = false

}

is my way is right or any other feasible way is present