2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-03 07:12:28 +00:00

Add QPDFArgParser::copyFromOtherTable

This commit is contained in:
Jay Berkenbilt 2022-01-06 14:26:32 -05:00
parent c3e9b64e7f
commit f1d805badc
8 changed files with 66 additions and 2 deletions

View File

@ -117,6 +117,14 @@ class QPDFArgParser
QPDF_DLL QPDF_DLL
void addRequiredChoices( void addRequiredChoices(
std::string const& arg, param_arg_handler_t, char const** choices); std::string const& arg, param_arg_handler_t, char const** choices);
QPDF_DLL
// If an option is shared among multiple tables and uses identical
// handlers, you can just copy it instead of repeating the
// registration call.
void copyFromOtherTable(std::string const& arg,
std::string const& other_table);
// The final check handler is called at the very end of argument // The final check handler is called at the very end of argument
// parsing. // parsing.
QPDF_DLL QPDF_DLL

View File

@ -153,6 +153,29 @@ QPDFArgParser::addRequiredChoices(
} }
} }
void
QPDFArgParser::copyFromOtherTable(std::string const& arg,
std::string const& other_table)
{
if (! this->m->option_tables.count(other_table))
{
QTC::TC("libtests", "QPDFArgParser copy from unknown");
throw std::logic_error(
"QPDFArgParser: attempt to copy from unknown table " +
other_table);
}
auto& ot = this->m->option_tables[other_table];
if (! ot.count(arg))
{
QTC::TC("libtests", "QPDFArgParser copy unknown");
throw std::logic_error(
"QPDFArgParser: attempt to copy unknown argument " + arg +
" from table " + other_table);
}
OptionEntry& oe = registerArg(arg);
oe = ot[arg];
}
void void
QPDFArgParser::addFinalCheck(bare_arg_handler_t handler) QPDFArgParser::addFinalCheck(bare_arg_handler_t handler)
{ {
@ -533,7 +556,8 @@ QPDFArgParser::parseArgs()
if (oep == this->m->option_table->end()) if (oep == this->m->option_table->end())
{ {
// This is registered automatically, so this can't happen. // This is registered automatically, so this can't happen.
throw std::logic_error("ArgParser: -- handler not registered"); throw std::logic_error(
"QPDFArgParser: -- handler not registered");
} }
} }
else if ((arg[0] == '-') && (strcmp(arg, "-") != 0)) else if ((arg[0] == '-') && (strcmp(arg, "-") != 0))

View File

@ -64,6 +64,10 @@ ArgParser::initOptions()
ap.registerOptionTable("baaa", nullptr); ap.registerOptionTable("baaa", nullptr);
ap.addBare("ewe", [this](){ output("you"); }); ap.addBare("ewe", [this](){ output("you"); });
ap.addBare("ram", [this](){ output("ram"); }); ap.addBare("ram", [this](){ output("ram"); });
ap.selectMainOptionTable();
ap.addBare("sheep", [this](){ this->ap.selectOptionTable("sheep"); });
ap.registerOptionTable("sheep", nullptr);
ap.copyFromOtherTable("ewe", "baaa");
} }
void void
@ -186,11 +190,28 @@ ArgParser::test_exceptions()
{ {
std::cout << "unknown table: " << e.what() << std::endl; std::cout << "unknown table: " << e.what() << std::endl;
} }
try
{
ap.copyFromOtherTable("one", "two");
assert(false);
}
catch (std::exception& e)
{
std::cout << "copy from unknown table: " << e.what() << std::endl;
}
try
{
ap.copyFromOtherTable("two", "baaa");
assert(false);
}
catch (std::exception& e)
{
std::cout << "copy unknown from other table: " << e.what() << std::endl;
}
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
ArgParser ap(argc, argv); ArgParser ap(argc, argv);
if ((argc == 2) && (strcmp(argv[1], "exceptions") == 0)) if ((argc == 2) && (strcmp(argv[1], "exceptions") == 0))
{ {

View File

@ -52,3 +52,5 @@ QPDFArgParser help option 0
QPDFArgParser positional 0 QPDFArgParser positional 0
QPDFArgParser unrecognized 0 QPDFArgParser unrecognized 0
QPDFArgParser complete choices 0 QPDFArgParser complete choices 0
QPDFArgParser copy from unknown 0
QPDFArgParser copy unknown 0

View File

@ -32,6 +32,7 @@ my @completion_tests = (
['arg_parser --quack "user password" ', undef, 'quack-x'], ['arg_parser --quack "user password" ', undef, 'quack-x'],
['arg_parser --quack "user pass\'word" ', undef, 'quack-x'], ['arg_parser --quack "user pass\'word" ', undef, 'quack-x'],
['arg_parser --quack user\ password ', undef, 'quack-x'], ['arg_parser --quack user\ password ', undef, 'quack-x'],
['arg_parser --sheep --', undef, 'sheep'],
); );
foreach my $c (@completion_tests) foreach my $c (@completion_tests)
@ -78,6 +79,7 @@ my @arg_tests = (
['@quack-xyz --', 0], # 15 ['@quack-xyz --', 0], # 15
['--salad', 2], # 16 ['--salad', 2], # 16
['--salad=spinach', 0], # 17 ['--salad=spinach', 0], # 17
['--sheep --ewe --', 0], # 18
); );
for (my $i = 0; $i < scalar(@arg_tests); ++$i) for (my $i = 0; $i < scalar(@arg_tests); ++$i)

View File

@ -0,0 +1,2 @@
you
total quacks: 0

View File

@ -0,0 +1,3 @@
--ewe
!--potato
!--ram

View File

@ -2,3 +2,5 @@ duplicate handler: QPDFArgParser: adding a duplicate handler for option potato i
duplicate handler: QPDFArgParser: adding a duplicate handler for option ram in baaa option table duplicate handler: QPDFArgParser: adding a duplicate handler for option ram in baaa option table
duplicate table: QPDFArgParser: registering already registered option table baaa duplicate table: QPDFArgParser: registering already registered option table baaa
unknown table: QPDFArgParser: selecting unregistered option table aardvark unknown table: QPDFArgParser: selecting unregistered option table aardvark
copy from unknown table: QPDFArgParser: attempt to copy from unknown table two
copy unknown from other table: QPDFArgParser: attempt to copy unknown argument two from table baaa