Changed argument handling a bit

This commit is contained in:
Rexy712 2019-05-19 12:17:38 -07:00
parent 0b3e857722
commit ad93eab52c
2 changed files with 79 additions and 73 deletions

View File

@ -22,15 +22,6 @@
#include <vector> //std::vector #include <vector> //std::vector
#include <cstddef> //size_t #include <cstddef> //size_t
struct cmd_options{
const char* lopt;
char sopt;
const char* desc;
};
extern const cmd_options cmd_arguments_list[];
extern const size_t cmd_arguments_list_size;
#define PRINT_LINE_NEVER 0 #define PRINT_LINE_NEVER 0
#define PRINT_LINE_ALWAYS 1 #define PRINT_LINE_ALWAYS 1
@ -70,6 +61,17 @@ struct cmd_args{
void set_nonprinting(bool = true); void set_nonprinting(bool = true);
}; };
struct cmd_options{
const char* lopt;
char sopt;
const char* desc;
};
extern const cmd_options cmd_arguments_list[];
extern const size_t cmd_arguments_list_size;
cmd_args process_cmd_args(int argc, char** argv); cmd_args process_cmd_args(int argc, char** argv);
#endif #endif

View File

@ -63,6 +63,10 @@ template<const char* S1, const char* S2>
struct concat_many_strs<S1,S2>{ struct concat_many_strs<S1,S2>{
static constexpr const char* str = concat_strs<S1, S2>::str; static constexpr const char* str = concat_strs<S1, S2>::str;
}; };
template<char... Cs>
struct char_to_str{
static constexpr const char str[sizeof...(Cs)+1] = {Cs..., '\0'};
};
#define NO_LONG_OPTION nullptr #define NO_LONG_OPTION nullptr
#define REQ_INTEGER_ARG "=<i>" #define REQ_INTEGER_ARG "=<i>"
@ -92,49 +96,49 @@ static constexpr const char IGNORE_ANIMATE_LONG_OPTION[] = "animate";
static constexpr const char IGNORE_DURATION_LONG_OPTION[] = "duration" REQ_INTEGER_ARG; static constexpr const char IGNORE_DURATION_LONG_OPTION[] = "duration" REQ_INTEGER_ARG;
static constexpr const char IGNORE_SPEED_LONG_OPTION[] = "speed" REQ_FLOAT_ARG; static constexpr const char IGNORE_SPEED_LONG_OPTION[] = "speed" REQ_FLOAT_ARG;
#define NO_SHORT_OPTION "" #define NO_SHORT_OPTION 0
static constexpr const char SHOW_ALL_SHORT_OPTION[] = "A"; static constexpr char SHOW_ALL_SHORT_OPTION = 'A';
static constexpr const char NUMBER_NONBLANK_SHORT_OPTION[] = "b"; static constexpr char NUMBER_NONBLANK_SHORT_OPTION = 'b';
static constexpr const char BINARY_SHORT_OPTION[] = "B"; static constexpr char BINARY_SHORT_OPTION = 'B';
static constexpr const char TRUECOLOR_SHORT_OPTION[] = "C"; static constexpr char TRUECOLOR_SHORT_OPTION = 'C';
static constexpr const char VE_SHORT_OPTION[] = "e"; static constexpr char VE_SHORT_OPTION = 'e';
static constexpr const char SHOW_ENDS_SHORT_OPTION[] = "E"; static constexpr char SHOW_ENDS_SHORT_OPTION = 'E';
static constexpr const char FORCE_COLOR_SHORT_OPTION[] = "f"; static constexpr char FORCE_COLOR_SHORT_OPTION = 'f';
static constexpr const char FREQ_SHORT_OPTION[] = "F"; static constexpr char FREQ_SHORT_OPTION = 'F';
static constexpr const char USAGE_SHORT_OPTION[] = "h"; static constexpr char USAGE_SHORT_OPTION = 'h';
static constexpr const char INVERT_SHORT_OPTION[] = "i"; static constexpr char INVERT_SHORT_OPTION = 'i';
static constexpr const char NUMBER_LINES_SHORT_OPTION[] = "n"; static constexpr char NUMBER_LINES_SHORT_OPTION = 'n';
static constexpr const char DISABLE_COLOR_SHORT_OPTION[] = "N"; static constexpr char DISABLE_COLOR_SHORT_OPTION = 'N';
static constexpr const char SPREAD_SHORT_OPTION[] = "p"; static constexpr char SPREAD_SHORT_OPTION = 'p';
static constexpr const char SQUEEZE_BLANKS_SHORT_OPTION[] = "s"; static constexpr char SQUEEZE_BLANKS_SHORT_OPTION = 's';
static constexpr const char SEED_SHORT_OPTION[] = "S"; static constexpr char SEED_SHORT_OPTION = 'S';
static constexpr const char VT_SHORT_OPTION[] = "t"; static constexpr char VT_SHORT_OPTION = 't';
static constexpr const char SHOW_TABS_SHORT_OPTION[] = "T"; static constexpr char SHOW_TABS_SHORT_OPTION = 'T';
static constexpr const char U_IGNORED_SHORT_OPTION[] = "u"; static constexpr char U_IGNORED_SHORT_OPTION = 'u';
static constexpr const char NONPRINTING_SHORT_OPTION[] = "v"; static constexpr char NONPRINTING_SHORT_OPTION = 'v';
static constexpr const char IGNORE_ANIMATE_SHORT_OPTION[] = "a"; static constexpr char IGNORE_ANIMATE_SHORT_OPTION = 'a';
static constexpr const char IGNORE_DURATION_SHORT_OPTION[] = "d"; static constexpr char IGNORE_DURATION_SHORT_OPTION = 'd';
static constexpr const char* IGNORE_SPEED_SHORT_OPTION = NO_SHORT_OPTION; static constexpr char IGNORE_SPEED_SHORT_OPTION = NO_SHORT_OPTION;
static constexpr const char* VERSION_SHORT_OPTION = NO_SHORT_OPTION; static constexpr char VERSION_SHORT_OPTION = NO_SHORT_OPTION;
#define NO_DESC nullptr #define NO_DESC nullptr
static constexpr const char IGNORED_DESC[] = "Ignored for compatability"; static constexpr const char IGNORED_DESC[] = "Ignored for compatability";
static constexpr const char EQUIV_DESC_BASE[] = "equivalent to -"; static constexpr const char EQUIV_DESC_BASE[] = "equivalent to -";
static constexpr const char* SHOW_ALL_DESC = concat_many_strs<EQUIV_DESC_BASE,NONPRINTING_SHORT_OPTION,SHOW_ENDS_SHORT_OPTION,SHOW_TABS_SHORT_OPTION>::str; static constexpr const char* SHOW_ALL_DESC = concat_many_strs<EQUIV_DESC_BASE,char_to_str<NONPRINTING_SHORT_OPTION>::str,char_to_str<SHOW_ENDS_SHORT_OPTION>::str,char_to_str<SHOW_TABS_SHORT_OPTION>::str>::str;
static constexpr const char NUMBER_NONBLANK_DESC_BASE[] = "number nonempty output lines, overrides -"; static constexpr const char NUMBER_NONBLANK_DESC_BASE[] = "number nonempty output lines, overrides -";
static constexpr const char* NUMBER_NONBLANK_DESC = concat_many_strs<NUMBER_NONBLANK_DESC_BASE,SHOW_TABS_SHORT_OPTION>::str; static constexpr const char* NUMBER_NONBLANK_DESC = concat_many_strs<NUMBER_NONBLANK_DESC_BASE,char_to_str<SHOW_TABS_SHORT_OPTION>::str>::str;
static constexpr const char* VE_DESC = concat_many_strs<EQUIV_DESC_BASE,NONPRINTING_SHORT_OPTION,SHOW_ENDS_SHORT_OPTION>::str; static constexpr const char* VE_DESC = concat_many_strs<EQUIV_DESC_BASE,char_to_str<NONPRINTING_SHORT_OPTION>::str,char_to_str<SHOW_ENDS_SHORT_OPTION>::str>::str;
static constexpr const char SHOW_ENDS_DESC[] = "display $ at end of each line"; static constexpr const char SHOW_ENDS_DESC[] = "display $ at end of each line";
static constexpr const char NUMBER_LINES_DESC[] = "number all output lines"; static constexpr const char NUMBER_LINES_DESC[] = "number all output lines";
static constexpr const char SQUEEZE_BLANKS_DESC[] = "suppress repeated empty output lines"; static constexpr const char SQUEEZE_BLANKS_DESC[] = "suppress repeated empty output lines";
static constexpr const char* VT_DESC = concat_many_strs<EQUIV_DESC_BASE,NONPRINTING_SHORT_OPTION,SHOW_TABS_SHORT_OPTION>::str; static constexpr const char* VT_DESC = concat_many_strs<EQUIV_DESC_BASE,char_to_str<NONPRINTING_SHORT_OPTION>::str,char_to_str<SHOW_TABS_SHORT_OPTION>::str>::str;
static constexpr const char SHOW_TABS_DESC[] = "display TAB characters as ^I"; static constexpr const char SHOW_TABS_DESC[] = "display TAB characters as ^I";
static constexpr const char* U_IGNORED_DESC = IGNORED_DESC; static constexpr const char* U_IGNORED_DESC = IGNORED_DESC;
static constexpr const char BINARY_DESC[] = "treat FILE as a binary file"; static constexpr const char BINARY_DESC[] = "treat FILE as a binary file\n";
static constexpr const char NONPRINTING_DESC_BASE[] = "Use ^ and M- notation, except for LFD and TAB. Implies -"; static constexpr const char NONPRINTING_DESC_BASE[] = "Use ^ and M- notation, except for LFD and TAB. Implies -";
static constexpr const char* NONPRINTING_DESC = concat_many_strs<NONPRINTING_DESC_BASE,BINARY_SHORT_OPTION>::str; static constexpr const char* NONPRINTING_DESC = concat_many_strs<NONPRINTING_DESC_BASE,char_to_str<BINARY_SHORT_OPTION>::str,char_to_str<'\n'>::str>::str;
static constexpr const char USAGE_DESC[] = "display this help and exit"; static constexpr const char USAGE_DESC[] = "display this help and exit";
static constexpr const char VERSION_DESC[] = "output version information and exit"; static constexpr const char VERSION_DESC[] = "output version information and exit";
static constexpr const char SEED_DESC[] = "Rainbow seed, 0 = random (default: 0)"; static constexpr const char SEED_DESC[] = "Rainbow seed, 0 = random (default: 0)";
@ -143,16 +147,46 @@ static constexpr const char FREQ_DESC[] = "Rainbow frequency
static constexpr const char TRUECOLOR_DESC[] = "24-bit (truecolor)"; static constexpr const char TRUECOLOR_DESC[] = "24-bit (truecolor)";
static constexpr const char FORCE_COLOR_DESC[] = "Force color even when stdout is not a tty"; static constexpr const char FORCE_COLOR_DESC[] = "Force color even when stdout is not a tty";
static constexpr const char DISABLE_COLOR_DESC_BASE[] = "Suppress color even when stdout is a tty, overrides -"; static constexpr const char DISABLE_COLOR_DESC_BASE[] = "Suppress color even when stdout is a tty, overrides -";
static constexpr const char* DISABLE_COLOR_DESC = concat_many_strs<DISABLE_COLOR_DESC_BASE,FORCE_COLOR_SHORT_OPTION>::str; static constexpr const char* DISABLE_COLOR_DESC = concat_many_strs<DISABLE_COLOR_DESC_BASE,char_to_str<FORCE_COLOR_SHORT_OPTION>::str>::str;
static constexpr const char INVERT_DESC[] = "Change background color instead of foreground"; static constexpr const char INVERT_DESC[] = "Change background color instead of foreground\n";
static constexpr const char* IGNORE_ANIMATE_DESC = IGNORED_DESC; static constexpr const char* IGNORE_ANIMATE_DESC = IGNORED_DESC;
static constexpr const char* IGNORE_DURATION_DESC = IGNORED_DESC; static constexpr const char* IGNORE_DURATION_DESC = IGNORED_DESC;
static constexpr const char* IGNORE_SPEED_DESC = IGNORED_DESC; static constexpr const char* IGNORE_SPEED_DESC = IGNORED_DESC;
#define SHORT_OPT(x) (x##_SHORT_OPTION)[0] #define SHORT_OPT(x) (x##_SHORT_OPTION)
#define OPTION(x) {x##_LONG_OPTION, SHORT_OPT(x), x##_DESC} #define OPTION(x) {x##_LONG_OPTION, SHORT_OPT(x), x##_DESC}
const cmd_options cmd_arguments_list[] = {
OPTION(SHOW_ALL),
OPTION(NUMBER_NONBLANK),
OPTION(VE),
OPTION(SHOW_ENDS),
OPTION(NUMBER_LINES),
OPTION(SQUEEZE_BLANKS),
OPTION(VT),
OPTION(SHOW_TABS),
OPTION(U_IGNORED),
OPTION(NONPRINTING),
OPTION(BINARY),
OPTION(SEED),
OPTION(SPREAD),
OPTION(FREQ),
OPTION(TRUECOLOR),
OPTION(FORCE_COLOR),
OPTION(DISABLE_COLOR),
OPTION(IGNORE_ANIMATE),
OPTION(IGNORE_DURATION),
OPTION(IGNORE_SPEED),
OPTION(INVERT),
OPTION(USAGE),
OPTION(VERSION)
};
const size_t cmd_arguments_list_size = sizeof(cmd_arguments_list)/sizeof(cmd_arguments_list[0]);
static constexpr bool is_integer(const char* s){ static constexpr bool is_integer(const char* s){
if(*s != '-' && (*s < '0' || *s > '9')){ if(*s != '-' && (*s < '0' || *s > '9')){
@ -195,6 +229,7 @@ static constexpr size_t strlen_pre_eq(const char* str){
return i; return i;
} }
#define CHECK_VALID_LONG_ARG(opt, off, arg, type) \ #define CHECK_VALID_LONG_ARG(opt, off, arg, type) \
{ \ { \
if(arg[off] != '=' || !is_##type(arg+off+1)){ \ if(arg[off] != '=' || !is_##type(arg+off+1)){ \
@ -383,34 +418,3 @@ cmd_args process_cmd_args(int argc, char** argv){
} }
return ret; return ret;
} }
const cmd_options cmd_arguments_list[] = {
OPTION(SHOW_ALL),
OPTION(NUMBER_NONBLANK),
OPTION(VE),
OPTION(SHOW_ENDS),
OPTION(NUMBER_LINES),
OPTION(SQUEEZE_BLANKS),
OPTION(VT),
OPTION(SHOW_TABS),
OPTION(U_IGNORED),
OPTION(NONPRINTING),
{nullptr,0,nullptr}, //add newline to output
OPTION(BINARY),
{nullptr,0,nullptr},
OPTION(SEED),
OPTION(SPREAD),
OPTION(FREQ),
OPTION(TRUECOLOR),
OPTION(FORCE_COLOR),
OPTION(DISABLE_COLOR),
OPTION(IGNORE_ANIMATE),
OPTION(IGNORE_DURATION),
OPTION(IGNORE_SPEED),
OPTION(INVERT),
{nullptr,0,nullptr},
OPTION(USAGE),
OPTION(VERSION)
};
const size_t cmd_arguments_list_size = sizeof(cmd_arguments_list)/sizeof(cmd_arguments_list[0]);