fixed a few CLI issues

This commit is contained in:
Andrei "Akari" Alexeyev 2017-04-03 02:10:16 +03:00
parent 630acf228b
commit b9d0e28fa0
2 changed files with 46 additions and 28 deletions

View file

@ -14,10 +14,10 @@ static void print_help(struct TsOption* opts) {
tsfprintf(stdout, "Usage: taisei [OPTIONS]\nTaisei is an open source Touhou clone.\n\nOptions:\n");
int margin = 20;
for(struct TsOption *opt = opts; opt->opt.name; opt++) {
tsfprintf(stdout, " -%c, --%s ",opt->opt.val,opt->opt.name);
tsfprintf(stdout, " -%c, --%s ", opt->opt.val,opt->opt.name);
int length = margin-(int)strlen(opt->opt.name);
if(opt->argname) {
tsfprintf(stdout, opt->argname);
tsfprintf(stdout, "%s", opt->argname);
length -= (int)strlen(opt->argname);
}
for(int i = 0; i < length; i++)
@ -25,8 +25,8 @@ static void print_help(struct TsOption* opts) {
if(opt->argname)
tsfprintf(stdout, opt->help, opt->argname);
else
tsfprintf(stdout, opt->help);
tsfprintf(stdout,"\n");
tsfprintf(stdout, "%s", opt->help);
tsfprintf(stdout, "\n");
}
}
@ -71,15 +71,22 @@ int cli_args(int argc, char **argv, CLIAction *a) {
int c;
int stageid = -1;
Character cha = -1;
ShotMode shot = -1;
while((c = getopt_long(argc, argv, optc ,opts, 0)) != -1) {
#define INVALID_CHAR ((Character)-1)
#define INVALID_SHOT ((ShotMode)-1)
// these may be unsigned depending on the compiler.
Character cha = INVALID_CHAR;
ShotMode shot = INVALID_SHOT;
while((c = getopt_long(argc, argv, optc, opts, 0)) != -1) {
char *endptr;
switch(c) {
case 'h':
case '?':
print_help(taisei_opts);
a->type = CLI_Quit;
// a->type = CLI_Quit;
exit(1);
break;
case 'r':
a->type = CLI_PlayReplay;
@ -97,12 +104,18 @@ int cli_args(int argc, char **argv, CLIAction *a) {
a->type = CLI_DumpStages;
break;
case 'd':
a->diff = D_Any;
for(int i = D_Easy ; i <= NUM_SELECTABLE_DIFFICULTIES; i++) {
if(strcasecmp(optarg,difficulty_name(i)) == 0) {
if(strcasecmp(optarg, difficulty_name(i)) == 0) {
a->diff = i;
break;
}
}
if(a->diff == D_Any) {
log_fatal("Invalid difficulty '%s'", optarg);
}
break;
case 's':
if(plrmode_parse(optarg,&cha,&shot))
@ -115,13 +128,14 @@ int cli_args(int argc, char **argv, CLIAction *a) {
if(stageid != -1 && a->type != CLI_PlayReplay && a->type != CLI_SelectStage)
log_warn("--sid was ignored");
if(stageid != -1 && !stage_get(stageid))
log_fatal("Invalid stage id: %x",stageid);
if(a->type != CLI_SelectStage && (cha != -1 || shot != -1))
if(a->type != CLI_SelectStage && (cha != INVALID_CHAR || shot != INVALID_SHOT))
log_warn("--shotmode was ignored");
if(cha != -1 && shot != -1) {
if(cha != INVALID_CHAR && shot != INVALID_SHOT) {
a->plrcha = cha;
a->plrshot = shot;
}

View file

@ -119,6 +119,26 @@ int main(int argc, char **argv) {
return 0;
}
// commandline arguments should be parsed as early as possible
CLIAction a;
cli_args(argc, argv, &a); // stage_init_array goes first!
if(a.type == CLI_Quit)
return 1;
if(a.type == CLI_DumpStages) {
stage_init_array();
for(StageInfo *stg = stages; stg->procs; ++stg) {
tsfprintf(stdout, "%x %s: %s\n", stg->id, stg->title, stg->subtitle);
}
stage_free_array();
return 0;
} else if(a.type == CLI_PlayReplay) {
if(!replay_load(&replay, a.filename, REPLAY_READ_ALL | REPLAY_READ_RAWPATH)) {
return 1;
}
}
log_info("Content path: %s", get_prefix());
log_info("Userdata path: %s", get_config_path());
@ -137,23 +157,6 @@ int main(int argc, char **argv) {
config_load(CONFIG_FILE);
log_lib_versions();
stage_init_array();
CLIAction a;
cli_args(argc, argv, &a); // stage_init_array goes first!
if(a.type == CLI_Quit)
return 1;
if(a.type == CLI_DumpStages) {
for(StageInfo *stg = stages; stg->procs; ++stg) {
tsfprintf(stdout, "%x %s: %s\n", stg->id, stg->title, stg->subtitle);
}
return 0;
} else if(a.type == CLI_PlayReplay) {
if(!replay_load(&replay, a.filename, REPLAY_READ_ALL | REPLAY_READ_RAWPATH)) {
return 1;
}
}
init_sdl();
init_global();
@ -163,6 +166,7 @@ int main(int argc, char **argv) {
audio_init();
load_resources();
gamepad_init();
stage_init_array();
progress_load(); // stage_init_array goes first!
set_transition(TransLoader, 0, FADE_TIME*2);