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:
parent
c3e9b64e7f
commit
f1d805badc
@ -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
|
||||||
|
@ -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))
|
||||||
|
@ -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))
|
||||||
{
|
{
|
||||||
|
@ -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
|
||||||
|
@ -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)
|
||||||
|
2
libtests/qtest/arg_parser/args-18.out
Normal file
2
libtests/qtest/arg_parser/args-18.out
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
you
|
||||||
|
total quacks: 0
|
3
libtests/qtest/arg_parser/completion-sheep.out
Normal file
3
libtests/qtest/arg_parser/completion-sheep.out
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
--ewe
|
||||||
|
!--potato
|
||||||
|
!--ram
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user