Improve command line interface.

This commit is contained in:
Steven Smith 2015-01-23 19:25:35 -08:00
parent 239547097f
commit 766aeba32f
4 changed files with 108 additions and 5 deletions

View file

@ -3,5 +3,5 @@ project(bannertool)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES source/main.cpp source/3ds/cbmd.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/lodepng/lodepng.cpp)
set(SOURCE_FILES source/main.cpp source/commandline.cpp source/3ds/cbmd.cpp source/3ds/lz11.cpp source/3ds/util.cpp source/lodepng/lodepng.cpp)
add_executable(bannertool ${SOURCE_FILES})

61
source/commandline.cpp Normal file
View file

@ -0,0 +1,61 @@
#include "commandline.h"
std::map<char*, char*, compare_strings> cmd_get_args(int argc, char* argv[]) {
std::map<char*, char*, compare_strings> args;
for(int i = 0; i < argc; i++) {
if((strncmp(argv[i], "-", 1) == 0 || strncmp(argv[i], "--", 2) == 0) && argc != i + 1) {
args.insert(std::pair<char*, char*>(argv[i], argv[i + 1]));
i++;
}
}
return args;
}
char* cmd_find_arg(std::map<char*, char*, compare_strings> args, const char* shortOpt, const char* longOpt) {
char sopt[strlen(shortOpt) + 2];
sprintf(sopt, "-%s", shortOpt);
char lopt[strlen(longOpt) + 3];
sprintf(lopt, "--%s", longOpt);
std::map<char*, char*, compare_strings>::iterator match = args.find(sopt);
if(match != args.end()) {
return (*match).second;
}
match = args.find(lopt);
if(match != args.end()) {
return (*match).second;
}
return NULL;
}
void cmd_print_usage(const char* executedFrom) {
printf("Usage: %s <command> <args>\n", executedFrom);
cmd_print_commands();
}
void cmd_print_info(const char* command) {
if(strcmp(command, "makebanner") == 0) {
printf("makebanner - Creates a .bnr file.\n");
printf(" -i/--image: PNG file to use as the banner image.\n");
printf(" -a/--audio: Audio file to use as the banner's tune.\n");
printf(" -o/--output: File to output the created banner to.\n");
}
}
void cmd_print_commands() {
printf("Available commands:\n");
cmd_print_info("makebanner");
}
void cmd_missing_args(const char* command) {
printf("Missing arguments for command \"%s\".\n", command);
cmd_print_info(command);
}
void cmd_invalid_command(const char* command) {
printf("Invalid command \"%s\".\n", command);
cmd_print_commands();
}

22
source/commandline.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef __COMMANDLINE_H__
#define __COMMANDLINE_H__
#include <string.h>
#include <map>
struct compare_strings {
bool operator()(char const *a, char const *b) {
return strcmp(a, b) < 0;
}
};
std::map<char*, char*, compare_strings> cmd_get_args(int argc, char* argv[]);
char* cmd_find_arg(std::map<char*, char*, compare_strings> args, const char* shortOpt, const char* longOpt);
void cmd_print_usage(const char* executedFrom);
void cmd_print_info(const char* command);
void cmd_print_commands();
void cmd_missing_args(const char* command);
void cmd_invalid_command(const char* command);
#endif

View file

@ -1,10 +1,13 @@
#include "data.h"
#include "3ds/3ds.h"
#include "data.h"
#include "commandline.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <map>
u8* convert_to_cgfx(const char* image, u32 width, u32 height, u32* size) {
u32 convertedSize = 0;
u8* converted = image_to_tiles(image, width, height, &convertedSize);
@ -73,13 +76,30 @@ int make_banner(const char* image, const char* audio, const char* output) {
fwrite(bnr, 1, bnrSize, fd);
fclose(fd);
free(bnr);
printf("Created banner \"%s\".", output);
return 0;
}
int main(int argc, char* argv[]) {
if(argc != 4) {
printf("Usage: %s <banner png> <audio bcwav> <output file>", argv[0]);
if(argc < 2) {
cmd_print_usage(argv[0]);
return -1;
}
return make_banner(argv[1], argv[2], argv[3]);
char* command = argv[1];
std::map<char*, char*, compare_strings> args = cmd_get_args(argc, argv);
if(strcmp(command, "makebanner") == 0) {
char* banner = cmd_find_arg(args, "i", "image");
char* audio = cmd_find_arg(args, "a", "audio");
char* output = cmd_find_arg(args, "o", "output");
if(!banner || !audio || !output) {
cmd_missing_args(command);
return -1;
}
return make_banner(banner, audio, output);
} else {
cmd_invalid_command(command);
return -1;
}
}