QPDFJob json: make bare arguments expect the empty string

Changing from bool requiring true to string requiring the empty string
is more consistent with the CLI and makes it possible to add an
optional parameter or choices later without breaking compatibility.
This commit is contained in:
Jay Berkenbilt 2022-01-31 18:15:10 -05:00
parent ea96330bb6
commit 21b9290785
21 changed files with 89 additions and 77 deletions

View File

@ -465,14 +465,19 @@ class Main:
self.json_init.append(
f'addBare([this]() {{ {config}->{flag_key}(); }});')
elif kind == 'optional_parameter' or kind == 'required_parameter':
# No optional parameters in json
# Optional parameters end up just being the empty string,
# so the handler has to deal with it. The empty string is
# also allowed for non-optional.
self.json_init.append(
f'addParameter([this](char const* p)'
f' {{ {config}->{flag_key}(p); }});')
elif kind == 'optional_choices' or kind == 'required_choices':
# No optional choices in json
elif kind == 'optional_choices':
self.json_init.append(
f'addChoices({v}_choices,'
f'addChoices({v}_choices, false,'
f' [this](char const* p) {{ {config}->{flag_key}(p); }});')
elif kind == 'required_choices':
self.json_init.append(
f'addChoices({v}_choices, true,'
f' [this](char const* p) {{ {config}->{flag_key}(p); }});')
def handle_json_manual(self, path):

View File

@ -1,5 +1,5 @@
# Generated by generate_auto_job
generate_auto_job 905fed38c06d258af617ac909bed78a7888a5eaba3f14cf143168665f6e90a86
generate_auto_job 8b3cc47eb96bfd003954d16d51c482e0e015544fdcdc1cbc89b6dcd964f8a2cb
include/qpdf/auto_job_c_att.hh 7ad43bb374c1370ef32ebdcdcb7b73a61d281f7f4e3f12755585872ab30fb60e
include/qpdf/auto_job_c_copy_att.hh 32275d03cdc69b703dd7e02ba0bbe15756e714e9ad185484773a6178dc09e1ee
include/qpdf/auto_job_c_enc.hh 72e138c7b96ed5aacdce78c1dec04b1c20d361faec4f8faf52f64c1d6be99265
@ -11,7 +11,7 @@ libqpdf/qpdf/auto_job_decl.hh 9f79396ec459f191be4c5fe34cf88c265cf47355a1a945fa39
libqpdf/qpdf/auto_job_help.hh a0ab6ab4dde2ad3d3f17ecae3ea274919119329e075061f3a3973535f5e367de
libqpdf/qpdf/auto_job_init.hh c244e03e8b83ed7db732920f40aff0134e5f2e78a6edb9473ea4dd1934a8953e
libqpdf/qpdf/auto_job_json_decl.hh c5e3fd38a3b0c569eb0c6b4c60953a09cd6bc7d3361a357a81f64fe36af2b0cf
libqpdf/qpdf/auto_job_json_init.hh bfaf88ad1461e1157e7a0eb6e8c90669eba6d03b5b1ffd2e7e7041250c5f0523
libqpdf/qpdf/auto_job_json_init.hh 3f86ce40931ca8f417d050fcd49104d73c1fa4e977ad19d54b372831a8ea17ed
libqpdf/qpdf/auto_job_schema.hh 2ec70dffdd15974d74102b4d7ada9f97449bc28c98be119efee5e15507ed22a8
manual/_ext/qpdf.py e9ac9d6c70642a3d29281ee5ad92ae2422dee8be9306fb8a0bc9dba0ed5e28f3
manual/cli.rst a75a7e34aa9aba4f06e9c88cae9a2d9a2aa4e55a08521dde1478e8f2d80aadab

View File

@ -108,7 +108,7 @@ QPDFJob::Config::coalesceContents()
QPDFJob::Config*
QPDFJob::Config::collate(char const* parameter)
{
auto n = ((parameter == 0) ? 1 :
auto n = (((parameter == 0) || (strlen(parameter) == 0)) ? 1 :
QUtil::string_to_uint(parameter));
o.m->collate = QIntC::to_size(n);
return this;
@ -519,7 +519,7 @@ QPDFJob::Config::showXref()
QPDFJob::Config*
QPDFJob::Config::splitPages(char const* parameter)
{
int n = ((parameter == 0) ? 1 :
int n = (((parameter == 0) || (strlen(parameter) == 0)) ? 1 :
QUtil::string_to_int(parameter));
o.m->split_pages = n;
return this;

View File

@ -31,7 +31,7 @@ namespace
void addBare(bare_handler_t);
void addParameter(param_handler_t);
void addChoices(char const** choices, param_handler_t);
void addChoices(char const** choices, bool required, param_handler_t);
void pushKey(std::string const& key);
void beginDict(json_handler_t start_fn,
bare_handler_t end_fn);
@ -106,11 +106,12 @@ Handlers::initHandlers()
void
Handlers::addBare(bare_handler_t fn)
{
jh->addBoolHandler([this, fn](std::string const& path, bool v){
if (! v)
jh->addStringHandler(
[this, fn](std::string const& path, std::string const& parameter){
if (! parameter.empty())
{
QTC::TC("qpdf", "QPDFJob json bare not true");
usage(path + ": value must be true");
QTC::TC("qpdf", "QPDFJob json bare not empty");
usage(path + ": value must be the empty string");
}
else
{
@ -129,22 +130,28 @@ Handlers::addParameter(param_handler_t fn)
}
void
Handlers::addChoices(char const** choices,
param_handler_t fn)
Handlers::addChoices(char const** choices, bool required, param_handler_t fn)
{
jh->addStringHandler(
[fn, choices, this](
[fn, choices, required, this](
std::string const& path, std::string const& parameter){
char const* p = parameter.c_str();
bool matches = false;
for (char const** i = choices; *i; ++i)
if ((! required) && (parameter.empty()))
{
if (strcmp(*i, p) == 0)
matches = true;
}
if (! matches)
{
for (char const** i = choices; *i; ++i)
{
QTC::TC("qpdf", "QPDFJob json choice match");
matches = true;
break;
if (strcmp(*i, p) == 0)
{
QTC::TC("qpdf", "QPDFJob json choice match");
matches = true;
break;
}
}
}
if (! matches)

View File

@ -43,19 +43,19 @@ pushKey("newlineBeforeEndstream");
addBare([this]() { c_main->newlineBeforeEndstream(); });
popHandler(); // key: newlineBeforeEndstream
pushKey("normalizeContent");
addChoices(yn_choices, [this](char const* p) { c_main->normalizeContent(p); });
addChoices(yn_choices, true, [this](char const* p) { c_main->normalizeContent(p); });
popHandler(); // key: normalizeContent
pushKey("streamData");
addChoices(stream_data_choices, [this](char const* p) { c_main->streamData(p); });
addChoices(stream_data_choices, true, [this](char const* p) { c_main->streamData(p); });
popHandler(); // key: streamData
pushKey("compressStreams");
addChoices(yn_choices, [this](char const* p) { c_main->compressStreams(p); });
addChoices(yn_choices, true, [this](char const* p) { c_main->compressStreams(p); });
popHandler(); // key: compressStreams
pushKey("recompressFlate");
addBare([this]() { c_main->recompressFlate(); });
popHandler(); // key: recompressFlate
pushKey("decodeLevel");
addChoices(decode_level_choices, [this](char const* p) { c_main->decodeLevel(p); });
addChoices(decode_level_choices, true, [this](char const* p) { c_main->decodeLevel(p); });
popHandler(); // key: decodeLevel
pushKey("decrypt");
addBare([this]() { c_main->decrypt(); });
@ -85,7 +85,7 @@ pushKey("linearizePass1");
addParameter([this](char const* p) { c_main->linearizePass1(p); });
popHandler(); // key: linearizePass1
pushKey("objectStreams");
addChoices(object_streams_choices, [this](char const* p) { c_main->objectStreams(p); });
addChoices(object_streams_choices, true, [this](char const* p) { c_main->objectStreams(p); });
popHandler(); // key: objectStreams
pushKey("minVersion");
addParameter([this](char const* p) { c_main->minVersion(p); });
@ -110,82 +110,82 @@ popHandler(); // key: ownerPassword
pushKey("40bit");
beginDict(bindJSON(&Handlers::beginEncrypt40bit), bindBare(&Handlers::endEncrypt40bit)); // .encrypt.40bit
pushKey("annotate");
addChoices(yn_choices, [this](char const* p) { c_enc->annotate(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->annotate(p); });
popHandler(); // key: annotate
pushKey("extract");
addChoices(yn_choices, [this](char const* p) { c_enc->extract(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->extract(p); });
popHandler(); // key: extract
pushKey("modify");
addChoices(modify128_choices, [this](char const* p) { c_enc->modify(p); });
addChoices(modify128_choices, true, [this](char const* p) { c_enc->modify(p); });
popHandler(); // key: modify
pushKey("print");
addChoices(print128_choices, [this](char const* p) { c_enc->print(p); });
addChoices(print128_choices, true, [this](char const* p) { c_enc->print(p); });
popHandler(); // key: print
popHandler(); // key: 40bit
pushKey("128bit");
beginDict(bindJSON(&Handlers::beginEncrypt128bit), bindBare(&Handlers::endEncrypt128bit)); // .encrypt.128bit
pushKey("accessibility");
addChoices(yn_choices, [this](char const* p) { c_enc->accessibility(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->accessibility(p); });
popHandler(); // key: accessibility
pushKey("annotate");
addChoices(yn_choices, [this](char const* p) { c_enc->annotate(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->annotate(p); });
popHandler(); // key: annotate
pushKey("assemble");
addChoices(yn_choices, [this](char const* p) { c_enc->assemble(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->assemble(p); });
popHandler(); // key: assemble
pushKey("cleartextMetadata");
addBare([this]() { c_enc->cleartextMetadata(); });
popHandler(); // key: cleartextMetadata
pushKey("extract");
addChoices(yn_choices, [this](char const* p) { c_enc->extract(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->extract(p); });
popHandler(); // key: extract
pushKey("form");
addChoices(yn_choices, [this](char const* p) { c_enc->form(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->form(p); });
popHandler(); // key: form
pushKey("modifyOther");
addChoices(yn_choices, [this](char const* p) { c_enc->modifyOther(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->modifyOther(p); });
popHandler(); // key: modifyOther
pushKey("modify");
addChoices(modify128_choices, [this](char const* p) { c_enc->modify(p); });
addChoices(modify128_choices, true, [this](char const* p) { c_enc->modify(p); });
popHandler(); // key: modify
pushKey("print");
addChoices(print128_choices, [this](char const* p) { c_enc->print(p); });
addChoices(print128_choices, true, [this](char const* p) { c_enc->print(p); });
popHandler(); // key: print
pushKey("forceV4");
addBare([this]() { c_enc->forceV4(); });
popHandler(); // key: forceV4
pushKey("useAes");
addChoices(yn_choices, [this](char const* p) { c_enc->useAes(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->useAes(p); });
popHandler(); // key: useAes
popHandler(); // key: 128bit
pushKey("256bit");
beginDict(bindJSON(&Handlers::beginEncrypt256bit), bindBare(&Handlers::endEncrypt256bit)); // .encrypt.256bit
pushKey("accessibility");
addChoices(yn_choices, [this](char const* p) { c_enc->accessibility(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->accessibility(p); });
popHandler(); // key: accessibility
pushKey("annotate");
addChoices(yn_choices, [this](char const* p) { c_enc->annotate(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->annotate(p); });
popHandler(); // key: annotate
pushKey("assemble");
addChoices(yn_choices, [this](char const* p) { c_enc->assemble(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->assemble(p); });
popHandler(); // key: assemble
pushKey("cleartextMetadata");
addBare([this]() { c_enc->cleartextMetadata(); });
popHandler(); // key: cleartextMetadata
pushKey("extract");
addChoices(yn_choices, [this](char const* p) { c_enc->extract(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->extract(p); });
popHandler(); // key: extract
pushKey("form");
addChoices(yn_choices, [this](char const* p) { c_enc->form(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->form(p); });
popHandler(); // key: form
pushKey("modifyOther");
addChoices(yn_choices, [this](char const* p) { c_enc->modifyOther(p); });
addChoices(yn_choices, true, [this](char const* p) { c_enc->modifyOther(p); });
popHandler(); // key: modifyOther
pushKey("modify");
addChoices(modify128_choices, [this](char const* p) { c_enc->modify(p); });
addChoices(modify128_choices, true, [this](char const* p) { c_enc->modify(p); });
popHandler(); // key: modify
pushKey("print");
addChoices(print128_choices, [this](char const* p) { c_enc->print(p); });
addChoices(print128_choices, true, [this](char const* p) { c_enc->print(p); });
popHandler(); // key: print
pushKey("allowInsecure");
addBare([this]() { c_enc->allowInsecure(); });
@ -238,11 +238,11 @@ pushKey("showAttachment");
addParameter([this](char const* p) { c_main->showAttachment(p); });
popHandler(); // key: showAttachment
pushKey("json");
addChoices(json_version_choices, [this](char const* p) { c_main->json(p); });
addChoices(json_version_choices, false, [this](char const* p) { c_main->json(p); });
popHandler(); // key: json
pushKey("jsonKey");
beginArray(bindJSON(&Handlers::beginJsonKeyArray), bindBare(&Handlers::endJsonKeyArray)); // .jsonKey[]
addChoices(json_key_choices, [this](char const* p) { c_main->jsonKey(p); });
addChoices(json_key_choices, true, [this](char const* p) { c_main->jsonKey(p); });
popHandler(); // array: .jsonKey[]
popHandler(); // key: jsonKey
pushKey("jsonObject");
@ -254,7 +254,7 @@ pushKey("allowWeakCrypto");
addBare([this]() { c_main->allowWeakCrypto(); });
popHandler(); // key: allowWeakCrypto
pushKey("keepFilesOpen");
addChoices(yn_choices, [this](char const* p) { c_main->keepFilesOpen(p); });
addChoices(yn_choices, true, [this](char const* p) { c_main->keepFilesOpen(p); });
popHandler(); // key: keepFilesOpen
pushKey("keepFilesOpenThreshold");
addParameter([this](char const* p) { c_main->keepFilesOpenThreshold(p); });
@ -272,7 +272,7 @@ pushKey("passwordIsHexKey");
addBare([this]() { c_main->passwordIsHexKey(); });
popHandler(); // key: passwordIsHexKey
pushKey("passwordMode");
addChoices(password_mode_choices, [this](char const* p) { c_main->passwordMode(p); });
addChoices(password_mode_choices, true, [this](char const* p) { c_main->passwordMode(p); });
popHandler(); // key: passwordMode
pushKey("suppressPasswordRecovery");
addBare([this]() { c_main->suppressPasswordRecovery(); });
@ -293,7 +293,7 @@ pushKey("iiMinBytes");
addParameter([this](char const* p) { c_main->iiMinBytes(p); });
popHandler(); // key: iiMinBytes
pushKey("removeUnreferencedResources");
addChoices(remove_unref_choices, [this](char const* p) { c_main->removeUnreferencedResources(p); });
addChoices(remove_unref_choices, true, [this](char const* p) { c_main->removeUnreferencedResources(p); });
popHandler(); // key: removeUnreferencedResources
pushKey("addAttachment");
beginArray(bindJSON(&Handlers::beginAddAttachmentArray), bindBare(&Handlers::endAddAttachmentArray)); // .addAttachment[]
@ -345,7 +345,7 @@ pushKey("collate");
addParameter([this](char const* p) { c_main->collate(p); });
popHandler(); // key: collate
pushKey("flattenAnnotations");
addChoices(flatten_choices, [this](char const* p) { c_main->flattenAnnotations(p); });
addChoices(flatten_choices, true, [this](char const* p) { c_main->flattenAnnotations(p); });
popHandler(); // key: flattenAnnotations
pushKey("flattenRotation");
addBare([this]() { c_main->flattenRotation(); });

View File

@ -630,7 +630,7 @@ qpdf check password password correct 0
qpdf check password not encrypted 0
QPDFJob_config password file 0
QPDFJob_config password stdin 0
QPDFJob json bare not true 0
QPDFJob json bare not empty 0
QPDFJob json choice mismatch 0
QPDFJob json choice match 0
QPDFJob json encrypt no key length 0

View File

@ -1,5 +1,5 @@
qpdf: error with job-json file bad-json-bare-option-false.json: .qdf: value must be true
qpdf: error with job-json file bad-json-bare-option-false.json: .qdf: value must be the empty string
Run qpdf--job-json-help for information on the file format.
For help:

View File

@ -1,3 +1,3 @@
{
"qdf": false
"qdf": "not-empty"
}

View File

@ -1,7 +1,7 @@
{
"inputFile": "minimal.pdf",
"outputFile": "a.pdf",
"staticId": true,
"staticId": "",
"addAttachment": [
{
"file": "auto-txt",

View File

@ -1,6 +1,6 @@
{
"inputFile": "minimal.pdf",
"outputFile": "a.pdf",
"deterministicId": true,
"deterministicId": "",
"objectStreams": "generate"
}

View File

@ -1,7 +1,7 @@
{
"inputFile": "minimal.pdf",
"outputFile": "a.pdf",
"staticId": true,
"staticId": "",
"copyAttachmentsFrom": [
{
"file": "job-json-add-attachments.pdf"

View File

@ -1,7 +1,7 @@
{
"empty": true,
"empty": "",
"outputFile": "a.pdf",
"staticId": true,
"staticId": "",
"pages": [
{
"file": "minimal.pdf"

View File

@ -1,8 +1,8 @@
{
"inputFile": "fxo-blue.pdf",
"outputFile": "a.pdf",
"staticId": true,
"staticAesIv": true,
"staticId": "",
"staticAesIv": "",
"encrypt": {
"userPassword": "u",
"ownerPassword": "o",

View File

@ -1,8 +1,8 @@
{
"inputFile": "minimal.pdf",
"outputFile": "a.pdf",
"staticId": true,
"staticAesIv": true,
"staticId": "",
"staticAesIv": "",
"encrypt": {
"userPassword": "u",
"ownerPassword": "o",

View File

@ -1,11 +1,11 @@
{
"inputFile": "minimal.pdf",
"outputFile": "a.pdf",
"staticId": true,
"staticId": "",
"encrypt": {
"userPassword": "u",
"ownerPassword": "o",
"40bit": {}
},
"allowWeakCrypto": true
"allowWeakCrypto": ""
}

View File

@ -2,7 +2,7 @@
"inputFile": "20-pages.pdf",
"password": "user",
"outputFile": "a.pdf",
"staticId": true,
"staticAesIv": true,
"staticId": "",
"staticAesIv": "",
"compressStreams": "n"
}

View File

@ -1,7 +1,7 @@
{
"inputFile": "minimal.pdf",
"outputFile": "a.pdf",
"staticId": true,
"linearize": true,
"staticId": "",
"linearize": "",
"compressStreams": "n"
}

View File

@ -1,6 +1,6 @@
{
"inputFile": "a.pdf",
"replaceInput": true,
"staticId": true,
"replaceInput": "",
"staticId": "",
"objectStreams": "generate"
}

View File

@ -1,7 +1,7 @@
{
"inputFile": "minimal.pdf",
"outputFile": "a.pdf",
"staticId": true,
"staticId": "",
"underlay": {
"file": "20-pages.pdf",
"password": "user",

View File

@ -2,8 +2,8 @@
"inputFile": "20-pages.pdf",
"password": "owner",
"outputFile": "a.pdf",
"staticId": true,
"decrypt": true,
"staticId": "",
"decrypt": "",
"underlay": {
"file": "fxo-green.pdf"
},

View File

@ -1,3 +1,3 @@
{
"showEncryption": true
"showEncryption": ""
}