some improvements on the dice command

This commit is contained in:
Florian Stinglmayr 2019-08-25 15:36:14 +02:00
parent 8c77114c01
commit 0f24c60a4e

View File

@ -7,6 +7,8 @@
#include <dice.h> #include <dice.h>
static bool prefix = false;
static bool evaluate_expression(char const *s) static bool evaluate_expression(char const *s)
{ {
dice_expression_t e = dice_expression_new(); dice_expression_t e = dice_expression_new();
@ -31,7 +33,11 @@ static bool evaluate_expression(char const *s)
goto error; goto error;
} }
if (prefix) {
printf("%s: %ld\n", s, result);
} else {
printf("%ld\n", result); printf("%ld\n", result);
}
ret = true; ret = true;
@ -43,9 +49,10 @@ error:
int main(int ac, char **av) int main(int ac, char **av)
{ {
static char const *optstr = "e:"; static char const *optstr = "e:p";
static struct option opts[] = { static struct option opts[] = {
{ "expression", required_argument, NULL, 'e' }, { "expression", required_argument, NULL, 'e' },
{ "prefix", no_argument, NULL, 'p' },
{ NULL, 0, NULL, 0} { NULL, 0, NULL, 0}
}; };
@ -64,6 +71,8 @@ int main(int ac, char **av)
} }
} break; } break;
case 'p': prefix = true; break;
default: default:
{ {
fprintf(stderr, "invalid option specified\n"); fprintf(stderr, "invalid option specified\n");
@ -83,8 +92,8 @@ int main(int ac, char **av)
for (i = optind; i < ac; i++) { for (i = optind; i < ac; i++) {
fprintf(f, "%s", av[i]); fprintf(f, "%s", av[i]);
if (i < ac) { if (i < ac-1) {
fputc(' ', f); putc(' ', f);
} }
} }