First try
This commit is contained in:
132
pkmn.c
Normal file
132
pkmn.c
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <argp.h>
|
||||||
|
|
||||||
|
const char *argp_program_version =
|
||||||
|
"pkmn 1.0";
|
||||||
|
const char *argp_program_bug_address =
|
||||||
|
"<gauvain.thomas@gmail.com>";
|
||||||
|
|
||||||
|
/* Program documentation. */
|
||||||
|
static char doc[] =
|
||||||
|
"Argp example #3 -- a program with options and arguments using argp";
|
||||||
|
|
||||||
|
/* A description of the arguments we accept. */
|
||||||
|
static char args_doc[] = "pokemon_name";
|
||||||
|
|
||||||
|
/* The options we understand. */
|
||||||
|
static struct argp_option options[] = {
|
||||||
|
{"random", 'r', 0, 0, "Use a random pokemon"},
|
||||||
|
{"shiney", 'S', 0, 0, "Use a shiney pokemon"},
|
||||||
|
{"verbose", 'v', 0, 0, "Produce verbose output" },
|
||||||
|
{"quiet", 'q', 0, 0, "Don't produce any output" },
|
||||||
|
{"silent", 's', 0, OPTION_ALIAS },
|
||||||
|
{ 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Used by main to communicate with parse_opt. */
|
||||||
|
struct arguments
|
||||||
|
{
|
||||||
|
char *args[1]; /* arg*/
|
||||||
|
int silent, verbose;
|
||||||
|
int random, shiney; /* random and shiney flags */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Parse a single option. */
|
||||||
|
static error_t
|
||||||
|
parse_opt (int key, char *arg, struct argp_state *state)
|
||||||
|
{
|
||||||
|
/* Get the input argument from argp_parse, which we
|
||||||
|
know is a pointer to our arguments structure. */
|
||||||
|
struct arguments *arguments = state->input;
|
||||||
|
|
||||||
|
switch (key)
|
||||||
|
{
|
||||||
|
case 'q': case 's':
|
||||||
|
arguments->silent = 1;
|
||||||
|
break;
|
||||||
|
case 'v':
|
||||||
|
arguments->verbose = 1;
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
printf("Random pokemon\n");
|
||||||
|
arguments->random = 1;
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
printf("Shiney pokemon\n");
|
||||||
|
arguments->shiney = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ARGP_KEY_ARG:
|
||||||
|
if (state->arg_num >= 1)
|
||||||
|
/* Too many arguments. */
|
||||||
|
argp_usage (state);
|
||||||
|
|
||||||
|
arguments->args[state->arg_num] = arg;
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ARGP_KEY_END:
|
||||||
|
if (state->arg_num < 0)
|
||||||
|
/* Not enough arguments. */
|
||||||
|
argp_usage (state);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return ARGP_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Our argp parser. */
|
||||||
|
static struct argp argp = { options, parse_opt, args_doc, doc };
|
||||||
|
|
||||||
|
int main (int argc, char **argv) {
|
||||||
|
struct arguments arguments;
|
||||||
|
char pkmn_path[] = "~/pokemons/";
|
||||||
|
|
||||||
|
/* Default values. */
|
||||||
|
arguments.silent = 0;
|
||||||
|
arguments.verbose = 0;
|
||||||
|
arguments.random = 0;
|
||||||
|
arguments.shiney = 0;
|
||||||
|
|
||||||
|
/* Parse our arguments; every option seen by parse_opt will
|
||||||
|
be reflected in arguments. */
|
||||||
|
argp_parse (&argp, argc, argv, 0, 0, &arguments);
|
||||||
|
|
||||||
|
if (arguments.verbose) {
|
||||||
|
printf("ARG1 = %s\n", arguments.args[0]);
|
||||||
|
printf("VERBOSE = %s\n", arguments.verbose ? "yes" : "no");
|
||||||
|
printf("SILENT = %s\n", arguments.silent ? "yes" : "no");
|
||||||
|
printf("RANDOM = %s\n", arguments.random ? "yes" : "no");
|
||||||
|
printf("SHINEY = %s\n", arguments.shiney ? "yes" : "no");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If random flag is set, roll a random pokemon
|
||||||
|
// Pokemons are located in ~/pokemons/
|
||||||
|
char pokemon[512];
|
||||||
|
if (arguments.random) {
|
||||||
|
system("ls ~/pokemons/yes | shuf -n 1 > /tmp/pkmn");
|
||||||
|
FILE *f = fopen("/tmp/pkmn", "r");
|
||||||
|
fscanf(f, "%s", pokemon);
|
||||||
|
fclose(f);
|
||||||
|
printf("Random pokemon: %s\n", pokemon);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then we can use the pokemon name to display it
|
||||||
|
// echo "$pokemon_name!" | cowsay -f "$cow_file" -W 70
|
||||||
|
char command[512];
|
||||||
|
if (arguments.random) {
|
||||||
|
sprintf(command, "echo \"%s!\" | cowsay -f ~/pokemons/yes/%s -W 70", pokemon, pokemon);
|
||||||
|
} else {
|
||||||
|
sprintf(command, "echo \"%s!\" | cowsay -f %s -W 70", arguments.args[0], "pikachu");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Command: %s\n", command);
|
||||||
|
|
||||||
|
if (!arguments.silent) {
|
||||||
|
system(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user