File: help_usage.cpp

package info (click to toggle)
cli11 2.6.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,520 kB
  • sloc: cpp: 26,743; python: 129; sh: 64; makefile: 11
file content (75 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause

#include <CLI/CLI.hpp>
#include <string>

int main(int argc, char **argv) {
    std::string input_file_name, output_file_name;
    int level{5}, subopt{0};

    // app caption
    CLI::App app{"CLI11 help"};

    app.require_subcommand(1);
    // subcommands options and flags
    CLI::App *const encode = app.add_subcommand("e", "encode")->ignore_case();  // ignore case
    encode->add_option("input", input_file_name, "input file")
        ->option_text(" ")
        ->required()
        ->check(CLI::ExistingFile);                                                               // file must exist
    encode->add_option("output", output_file_name, "output file")->option_text(" ")->required();  // required option
    encode->add_option("-l, --level", level, "encoding level")
        ->option_text("[1..9]")
        ->check(CLI::Range(1, 9))
        ->default_val(5);                                   // limit parameter range
    encode->add_flag("-R, --remove", "remove input file");  // no parameter option
    encode->add_flag("-s, --suboption", subopt, "suboption")->option_text(" ");

    CLI::App *const decode = app.add_subcommand("d", "decode")->ignore_case();
    decode->add_option("input", input_file_name, "input file")->option_text(" ")->required()->check(CLI::ExistingFile);
    decode->add_option("output", output_file_name, "output file")->option_text(" ")->required();

    // Usage message modification
    std::string usage_msg = "Usage: " + std::string(argv[0]) + " <command> [options] <input-file> <output-file>";
    app.usage(usage_msg);
    // flag to display full help at once
    app.set_help_flag("");
    app.set_help_all_flag("-h, --help");

    CLI11_PARSE(app, argc, argv);

    return 0;
}

/*
$ ./help_usage -h
  CLI11 help

OPTIONS:
  -h,     --help

SUBCOMMANDS:
e
  encode

POSITIONALS:
  input                       input file
  output                      output file

OPTIONS:
  -l,     --level [1..9]      encoding level
  -R,     --remove            remove input file
  -s,     --suboption         suboption


d
  decode

POSITIONALS:
  input                       input file
  output                      output file
*/