File: test_parent_parsers.cpp

package info (click to toggle)
libargparse 3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: cpp: 11,319; python: 8; makefile: 4; sh: 3
file content (40 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download | duplicates (5)
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
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>

using doctest::test_suite;

TEST_CASE("Add parent parsers" * test_suite("parent_parsers")) {
  argparse::ArgumentParser parent_parser("main");
  parent_parser.add_argument("--verbose")
      .default_value(false)
      .implicit_value(true);

  argparse::ArgumentParser child_parser("foo");
  child_parser.add_parents(parent_parser);
  child_parser.parse_args({"./main", "--verbose"});
  REQUIRE(child_parser["--verbose"] == true);
  REQUIRE(parent_parser["--verbose"] == false);
}

TEST_CASE("Add parent to multiple parent parsers" *
          test_suite("parent_parsers")) {
  argparse::ArgumentParser parent_parser("main");
  parent_parser.add_argument("--parent").default_value(0).scan<'i', int>();

  argparse::ArgumentParser foo_parser("foo");
  foo_parser.add_argument("foo");
  foo_parser.add_parents(parent_parser);
  foo_parser.parse_args({"./main", "--parent", "2", "XXX"});
  REQUIRE(foo_parser["--parent"] == 2);
  REQUIRE(foo_parser["foo"] == std::string("XXX"));
  REQUIRE(parent_parser["--parent"] == 0);

  argparse::ArgumentParser bar_parser("bar");
  bar_parser.add_argument("--bar");
  bar_parser.parse_args({"./main", "--bar", "YYY"});
  REQUIRE(bar_parser["--bar"] == std::string("YYY"));
}