mirror of
https://github.com/fmang/opustags.git
synced 2025-01-28 19:05:03 +01:00
Factor CLI argument transcoding
This commit is contained in:
parent
fd5fa3cd5f
commit
6db7f07bd5
27
src/cli.cc
27
src/cli.cc
@ -60,7 +60,7 @@ ot::status ot::parse_options(int argc, char** argv, ot::options& opt, FILE* comm
|
||||
{
|
||||
static ot::encoding_converter to_utf8("", "UTF-8");
|
||||
std::string utf8;
|
||||
std::string::size_type equal;
|
||||
const char* equal;
|
||||
ot::status rc;
|
||||
bool set_all = false;
|
||||
opt = {};
|
||||
@ -86,21 +86,16 @@ ot::status ot::parse_options(int argc, char** argv, ot::options& opt, FILE* comm
|
||||
opt.overwrite = true;
|
||||
break;
|
||||
case 'd':
|
||||
rc = to_utf8(optarg, utf8);
|
||||
if (rc != ot::st::ok)
|
||||
return {st::bad_arguments, "Could not encode argument into UTF-8: " + rc.message};
|
||||
opt.to_delete.emplace_back(std::move(utf8));
|
||||
opt.to_delete.emplace_back(optarg);
|
||||
break;
|
||||
case 'a':
|
||||
case 's':
|
||||
rc = to_utf8(optarg, utf8);
|
||||
if (rc != ot::st::ok)
|
||||
return {st::bad_arguments, "Could not encode argument into UTF-8: " + rc.message};
|
||||
if ((equal = utf8.find('=')) == std::string::npos)
|
||||
equal = strchr(optarg, '=');
|
||||
if (equal == nullptr)
|
||||
return {st::bad_arguments, "Comment does not contain an equal sign: "s + optarg + "."};
|
||||
if (c == 's')
|
||||
opt.to_delete.emplace_back(utf8.substr(0, equal));
|
||||
opt.to_add.emplace_back(std::move(utf8));
|
||||
opt.to_delete.emplace_back(optarg, equal - optarg);
|
||||
opt.to_add.emplace_back(optarg);
|
||||
break;
|
||||
case 'S':
|
||||
opt.delete_all = true;
|
||||
@ -130,6 +125,16 @@ ot::status ot::parse_options(int argc, char** argv, ot::options& opt, FILE* comm
|
||||
opt.paths_in.emplace_back(argv[i]);
|
||||
}
|
||||
|
||||
// Convert arguments to UTF-8.
|
||||
for (std::list<std::string>* args : { &opt.to_add, &opt.to_delete }) {
|
||||
for (std::string& arg : *args) {
|
||||
rc = to_utf8(arg, utf8);
|
||||
if (rc != ot::st::ok)
|
||||
return {st::bad_arguments, "Could not encode argument into UTF-8: " + rc.message};
|
||||
arg = std::move(utf8);
|
||||
}
|
||||
}
|
||||
|
||||
if (opt.in_place && opt.path_out)
|
||||
return {st::bad_arguments, "Cannot combine --in-place and --output."};
|
||||
|
||||
|
@ -450,7 +450,7 @@ struct options {
|
||||
*
|
||||
* Option: --delete, --set
|
||||
*/
|
||||
std::vector<std::string> to_delete;
|
||||
std::list<std::string> to_delete;
|
||||
/**
|
||||
* Delete all the existing comments.
|
||||
*
|
||||
|
2
t/cli.cc
2
t/cli.cc
@ -71,7 +71,7 @@ void check_good_arguments()
|
||||
opt = parse({"opustags", "x", "--output", "y", "-D", "-s", "X=Y Z", "-d", "a=b"});
|
||||
if (opt.paths_in.size() != 1 || opt.paths_in.front() != "x" || !opt.path_out ||
|
||||
opt.path_out != "y" || !opt.delete_all || opt.overwrite || opt.to_delete.size() != 2 ||
|
||||
opt.to_delete[0] != "X" || opt.to_delete[1] != "a=b" ||
|
||||
opt.to_delete.front() != "X" || *std::next(opt.to_delete.begin()) != "a=b" ||
|
||||
opt.to_add != std::list<std::string>{"X=Y Z"})
|
||||
throw failure("unexpected option parsing result for case #1");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user