main: option parsing
This commit is contained in:
parent
0791433a4e
commit
8e3fc91ccc
124
src/main.cpp
124
src/main.cpp
|
@ -40,8 +40,8 @@ void print_help (char*pname)
|
||||||
out (" -T, --test perform (probably nonexistent) testing/debugging stuff");
|
out (" -T, --test perform (probably nonexistent) testing/debugging stuff");
|
||||||
outeol;
|
outeol;
|
||||||
out ("Global options:");
|
out ("Global options:");
|
||||||
out (" -i, --input input file, default is stdin");
|
out (" -R, --in input file, default is stdin");
|
||||||
out (" -o, --output output file, default is stdout");
|
out (" -o, --out output file, default is stdout");
|
||||||
out (" -a, --armor use ascii-armored I/O");
|
out (" -a, --armor use ascii-armored I/O");
|
||||||
out (" -y, --yes assume that answer is `yes' everytime");
|
out (" -y, --yes assume that answer is `yes' everytime");
|
||||||
outeol;
|
outeol;
|
||||||
|
@ -72,7 +72,8 @@ void print_help (char*pname)
|
||||||
outeol;
|
outeol;
|
||||||
out ("Key management options:");
|
out ("Key management options:");
|
||||||
out (" -n, --no-action on import, only show what would be imported");
|
out (" -n, --no-action on import, only show what would be imported");
|
||||||
out (" -N, --name specify a new name for renaming");
|
out (" -N, --name specify a new name for renaming or importing");
|
||||||
|
out (" -F, --filter only work with keys with matching names");
|
||||||
out (" -f, --fingerprint format key IDs nicely for human eyes");
|
out (" -f, --fingerprint format key IDs nicely for human eyes");
|
||||||
outeol;
|
outeol;
|
||||||
out ("Codecrypt eats data. Use it with caution.");
|
out ("Codecrypt eats data. Use it with caution.");
|
||||||
|
@ -108,16 +109,19 @@ int main (int argc, char**argv)
|
||||||
do_test = false,
|
do_test = false,
|
||||||
has_opt = false,
|
has_opt = false,
|
||||||
opt_armor = false,
|
opt_armor = false,
|
||||||
|
opt_yes = false,
|
||||||
opt_fingerprint = false,
|
opt_fingerprint = false,
|
||||||
opt_clearsign = false,
|
opt_clearsign = false,
|
||||||
opt_import_no_action = false;
|
opt_import_no_action = false;
|
||||||
|
|
||||||
std::string recipient, user,
|
std::string recipient, user,
|
||||||
input, output,
|
input, output,
|
||||||
name,
|
name, filter,
|
||||||
action, action_param,
|
action_param,
|
||||||
detach_sign;
|
detach_sign;
|
||||||
|
|
||||||
|
char action = 0;
|
||||||
|
|
||||||
//process options
|
//process options
|
||||||
int c, option_index;
|
int c, option_index;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -133,25 +137,26 @@ int main (int argc, char**argv)
|
||||||
{"user", 1, 0, 'u' },
|
{"user", 1, 0, 'u' },
|
||||||
|
|
||||||
//I/O redirection from default stdin/out
|
//I/O redirection from default stdin/out
|
||||||
{"input", 1, 0, 'i' },
|
{"in", 1, 0, 'R' },
|
||||||
{"output", 1, 0, 'o' },
|
{"out", 1, 0, 'o' },
|
||||||
|
|
||||||
//keyring management
|
//keyring management
|
||||||
{"list", 2, 0, 'k' },
|
{"list", 0, 0, 'k' },
|
||||||
{"import", 2, 0, 'i' },
|
{"import", 0, 0, 'i' },
|
||||||
{"export", 2, 0, 'p' },
|
{"export", 0, 0, 'p' },
|
||||||
{"delete", 1, 0, 'x' },
|
{"delete", 1, 0, 'x' },
|
||||||
|
{"rename", 1, 0, 'm' },
|
||||||
|
|
||||||
{"list-secret", 2, 0, 'K' },
|
{"list-secret", 0, 0, 'K' },
|
||||||
{"import-secret", 2, 0, 'I' },
|
{"import-secret", 0, 0, 'I' },
|
||||||
{"export-secret", 2, 0, 'P' },
|
{"export-secret", 0, 0, 'P' },
|
||||||
{"delete-secret", 1, 0, 'X' },
|
{"delete-secret", 1, 0, 'X' },
|
||||||
|
{"rename-secret", 1, 0, 'M' },
|
||||||
|
|
||||||
{"gen-key", 1, 0, 'g' },
|
{"gen-key", 1, 0, 'g' },
|
||||||
|
|
||||||
{"rename", 1, 0, 'm' },
|
|
||||||
{"rename-secret", 1, 0, 'M' },
|
|
||||||
{"name", 1, 0, 'N' },
|
{"name", 1, 0, 'N' },
|
||||||
|
{"filter", 1, 0, 'F' },
|
||||||
|
|
||||||
{"fingerprint", 0, 0, 'f' },
|
{"fingerprint", 0, 0, 'f' },
|
||||||
{"no-action", 0, 0, 'n' },
|
{"no-action", 0, 0, 'n' },
|
||||||
|
@ -169,9 +174,10 @@ int main (int argc, char**argv)
|
||||||
{0, 0, 0, 0 }
|
{0, 0, 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
option_index = -1;
|
||||||
c = getopt_long
|
c = getopt_long
|
||||||
(argc, argv,
|
(argc, argv,
|
||||||
"hVTayr:u:i:o:k::i::p::x:K::I::P::X:g:m:M:N:fnsvedCb:",
|
"hVTayr:u:R:o:kipx:m:KIPX:M:g:N:F:fnsvedCb:",
|
||||||
long_opts, &option_index);
|
long_opts, &option_index);
|
||||||
if (c == -1) break;
|
if (c == -1) break;
|
||||||
|
|
||||||
|
@ -182,18 +188,92 @@ int main (int argc, char**argv)
|
||||||
case 'h':
|
case 'h':
|
||||||
do_help = true;
|
do_help = true;
|
||||||
break;
|
break;
|
||||||
case 'V':
|
|
||||||
do_version = true;
|
#define read_flag(ch,var) case ch: var=true; break;
|
||||||
break;
|
|
||||||
case 'T':
|
#define read_single_opt(ch,var,errmsg) \
|
||||||
do_test = true;
|
case ch: if(var.length()) {progerr(errmsg); do_help=true;}\
|
||||||
break;
|
else var=optarg; break;
|
||||||
|
|
||||||
|
#define read_action(ch) read_action_comb(ch,0,0)
|
||||||
|
|
||||||
|
#define read_action_comb(ch, hit, comb) \
|
||||||
|
case ch: \
|
||||||
|
if(hit && action==hit) { \
|
||||||
|
action=comb; \
|
||||||
|
if(optarg) action_param=optarg; \
|
||||||
|
} else if(action) { \
|
||||||
|
progerr("please specify a single action"); \
|
||||||
|
do_help=true; \
|
||||||
|
} else { \
|
||||||
|
action=ch; \
|
||||||
|
if(optarg) action_param=optarg; \
|
||||||
|
} break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
read_flag ('V', do_version)
|
||||||
|
read_flag ('T', do_test)
|
||||||
|
read_flag ('a', opt_armor)
|
||||||
|
read_flag ('y', opt_yes)
|
||||||
|
|
||||||
|
read_single_opt ('r', recipient,
|
||||||
|
"specify only one recipient")
|
||||||
|
read_single_opt ('u', user,
|
||||||
|
"specify only one local user")
|
||||||
|
read_single_opt ('R', input,
|
||||||
|
"cannot accept multiple inputs")
|
||||||
|
read_single_opt ('o', output,
|
||||||
|
"cannot accept multiple outputs")
|
||||||
|
|
||||||
|
read_action ('k')
|
||||||
|
read_action ('i')
|
||||||
|
read_action ('p')
|
||||||
|
read_action ('x')
|
||||||
|
read_action ('m')
|
||||||
|
|
||||||
|
read_action ('K')
|
||||||
|
read_action ('I')
|
||||||
|
read_action ('P')
|
||||||
|
read_action ('X')
|
||||||
|
read_action ('M')
|
||||||
|
|
||||||
|
read_action ('g')
|
||||||
|
|
||||||
|
read_single_opt ('N', name,
|
||||||
|
"please specify single name")
|
||||||
|
read_single_opt ('F', filter,
|
||||||
|
"please specify single filter string")
|
||||||
|
|
||||||
|
read_flag ('f', opt_fingerprint)
|
||||||
|
read_flag ('n', opt_import_no_action)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* combinations of s+e and d+v are possible. result is
|
||||||
|
* 'E' = "big encrypt with sig" and 'D' "big decrypt
|
||||||
|
* with verify".
|
||||||
|
*/
|
||||||
|
read_action_comb ('s', 'e', 'E')
|
||||||
|
read_action_comb ('v', 'd', 'D')
|
||||||
|
read_action_comb ('e', 's', 'E')
|
||||||
|
read_action_comb ('d', 'v', 'D')
|
||||||
|
|
||||||
|
read_flag ('C', opt_clearsign)
|
||||||
|
read_single_opt ('b', detach_sign,
|
||||||
|
"specify only one detach-sign file")
|
||||||
|
|
||||||
|
#undef read_flag
|
||||||
|
#undef read_single_opt
|
||||||
|
#undef read_action
|
||||||
|
|
||||||
default: //which doesn't just happen.
|
default: //which doesn't just happen.
|
||||||
|
do_help = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optind != argc) {
|
if (optind != argc) {
|
||||||
|
for (int i = 0; i < argc; ++i) out (argv[i]);
|
||||||
err (argv[0] << ": unmatched non-option parameters");
|
err (argv[0] << ": unmatched non-option parameters");
|
||||||
do_help = true;
|
do_help = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,5 +29,6 @@
|
||||||
#define outeol std::cout << std::endl
|
#define outeol std::cout << std::endl
|
||||||
#define err(x) std::cerr << x << std::endl
|
#define err(x) std::cerr << x << std::endl
|
||||||
#define erreol std::cerr << std::endl
|
#define erreol std::cerr << std::endl
|
||||||
|
#define progerr(x) std::cerr << argv[0] << ": " << x << std::endl
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue