Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to make multi command and show them on help function?



  • $ cli --help
    Usage: root [OPTIONS] COMMAND [ARGS]...
    
    Options:
      --help  Show this message and exit.
    
    Commands:
      cmd1  Command on cli1
      cmd2  Command on cli2
    

    How to implement for this help function.


  • Qt Champions 2019

    @Kien-Bui What is cli1 and cli2?


  • Lifetime Qt Champion

    @Kien-Bui

    A good example for that is canbusutil

    This is the help output:

    Invalid number of arguments (0 given).
    
    Usage: canbusutil [options] plugin device [data]
    Sends arbitrary CAN bus frames.
    If the -l option is set, all received CAN bus frames are dumped.
    
    Options:
      -h, --help          Displays this help.
      -v, --version       Displays version information.
      -l, --listen        Start listening CAN data on device.
      -L, --list-plugins  List all available plugins.
      -t, --timestamp     Show timestamp for each received CAN bus frame.
      -i, --info          Show extra info (CAN FD flags) for each received CAN bus
                          frame.
      -d, --devices       Show available CAN bus devices for the given plugin.
    
    Arguments:
      plugin              Plugin name to use. See --list-plugins.
      device              Device to use.
      data                Data to send if -l is not specified. Format:
                          	<id>#{payload}          (CAN 2.0 data frames),
                          	<id>#Rxx                (CAN 2.0 RTR frames with xx bytes
                          data length),
                          	<id>##[flags]{payload}  (CAN FD data frames),
                          where {payload} has 0..8 (0..64 CAN FD) ASCII hex-value
                          pairs, and flags is one optional ASCII hex char for CAN FD
                          flags: 1 = Bitrate Switch, 2 = Error State Indicator
                          e.g. 1#1a2b3c
    


  • @aha_1980
    But in Usage part

    Usage: canbusutil [options] plugin device [data]
    

    plugin, device: They can't both exist on a command


  • Lifetime Qt Champion

    @Kien-Bui

    Do you want to have something like git?

    git pull [options]
    git rebase branch [options]
    git checkout branch [options]
    git cherry-pick hast [options]
    

    ?

    I think you have to set up different QCommandLineParsers for that, because the help and the options change depending on the sub-command (pull, rebase, checkout, cherry-pick).

    So you would have to parse the sub-command (positional argument) and then parse the rest of the command line to see if all arguments and options are correct.


Log in to reply