Clap (derive) - Add custom section for documentation

⚓ Rust    📅 2026-01-15    👤 surdeus    👁️ 1      

surdeus

I have a use case where I want to group keyword arguments into groups in order to display them in separate section when running my_program --help.

I read about ArgGroups at clap::_derive::_tutorial - Rust but could not find what I was looking for. I don't need any relationships or dependencies between the keyword arguments. I just want to group them into sections for documentation purposes when printing --help to have a better overview - they don't exclude one another and can be used together in whatever order.

In my concrete example I want:

Command line arguments

Usage: my_program [OPTIONS] <TTY>

Arguments:
  <TTY>  Path to the serial TTY device

Options:
  -h, --help
          Print help

Zigbee options:
  -r, --reinitialize
          Whether to reinitialize the device
  -L, --link-key <LINK_KEY>
          The link key for the new network [default: foo]
  -N, --network-key <NETWORK_KEY>
          The network key for the new network [default: bar]
  -P, --pan-id <PAN_ID>
          The PAN ID for the new network [default: 1234]
  -E, --extended-pan-id <EXTENDED_PAN_ID>
          The Extended PAN ID for the new network [default: 00:00:00:00:00:00:00:00]
      --protocol-version <PROTOCOL_VERSION>
          The protocol version to use [default: 8]
  -s, --channel-size <CHANNEL_SIZE>
          The channel size for communication with the device [default: 64]

Radio options:
  -c, --channel <CHANNEL>
          The radio channel for the new network [default: 12]
  -p, --tx-power <TX_POWER>
          The radio transmit power for the device [default: 8]
  -b, --tx-power-mode-boost
          Whether to use the TX power boost mode

Concentrator options:
      --min-time <MIN_TIME>
          Minimum time (in seconds) for concentrator [default: 1m]
      --max-time <MAX_TIME>
          Maximum time (in seconds) for concentrator [default: 1h]
      --route-error-threshold <ROUTE_ERROR_THRESHOLD>
          Route error threshold for concentrator [default: 8]
      --delivery-failure-threshold <DELIVERY_FAILURE_THRESHOLD>
          Delivery failure threshold for concentrator [default: 8]
      --max-hops <MAX_HOPS>
          Maximum hops for concentrator [default: 0]

Web server options:
  -a, --listen-address <LISTEN_ADDRESS>
          The IP address to listen on [default: 0.0.0.0:9000]

Instead of

Command line arguments

Usage: my_program [OPTIONS] <TTY>

Arguments:
  <TTY>  Path to the serial TTY device

Options:
  -r, --reinitialize
          Whether to reinitialize the device
  -L, --link-key <LINK_KEY>
          The link key for the new network [default: foo]
  -N, --network-key <NETWORK_KEY>
          The network key for the new network [default: bar]
  -P, --pan-id <PAN_ID>
          The PAN ID for the new network [default: 1234]
  -E, --extended-pan-id <EXTENDED_PAN_ID>
          The Extended PAN ID for the new network [default: 00:00:00:00:00:00:00:00]
      --protocol-version <PROTOCOL_VERSION>
          The protocol version to use [default: 8]
  -s, --channel-size <CHANNEL_SIZE>
          The channel size for communication with the device [default: 64]
  -c, --channel <CHANNEL>
          The radio channel for the new network [default: 12]
  -p, --tx-power <TX_POWER>
          The radio transmit power for the device [default: 8]
  -b, --tx-power-mode-boost
          Whether to use the TX power boost mode
      --min-time <MIN_TIME>
          Minimum time (in seconds) for concentrator [default: 1m]
      --max-time <MAX_TIME>
          Maximum time (in seconds) for concentrator [default: 1h]
      --route-error-threshold <ROUTE_ERROR_THRESHOLD>
          Route error threshold for concentrator [default: 8]
      --delivery-failure-threshold <DELIVERY_FAILURE_THRESHOLD>
          Delivery failure threshold for concentrator [default: 8]
      --max-hops <MAX_HOPS>
          Maximum hops for concentrator [default: 0]
  -a, --listen-address <LISTEN_ADDRESS>
          The IP address to listen on [default: 0.0.0.0:9000]
  -h, --help
          Print help

2 posts - 1 participant

Read full topic

🏷️ Rust_feed