feat(args): $0, $1, ... in vars
This commit is contained in:
parent
82f35cfa08
commit
7bcf48f63e
4 changed files with 82 additions and 29 deletions
|
|
@ -4,13 +4,22 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int args_handler(int argc, char **argv, struct args_options *options)
|
||||
#include "../string_utils/string_utils.h"
|
||||
#include "../vars/vars.h"
|
||||
|
||||
int args_handler(int argc, char **argv, struct args_options *options,
|
||||
struct hash_map *vars)
|
||||
{
|
||||
options->type = INPUT_UNDEFINED;
|
||||
options->input_source = NULL;
|
||||
options->pretty_print = false;
|
||||
options->verbose = false;
|
||||
|
||||
int arg_index = 1;
|
||||
char index_str[11];
|
||||
|
||||
set_var_copy(vars, "0", argv[0]);
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
if (strcmp(argv[i], "--pretty-print") == 0)
|
||||
|
|
@ -44,18 +53,18 @@ int args_handler(int argc, char **argv, struct args_options *options)
|
|||
fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
else if (options->type == INPUT_UNDEFINED)
|
||||
{
|
||||
if (options->type != INPUT_UNDEFINED)
|
||||
{
|
||||
fprintf(stderr, "Multiple input sources specified: %s\n",
|
||||
argv[i]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
options->type = INPUT_FILE;
|
||||
options->input_source = argv[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
// All remaining arguments are treated as additional arguments
|
||||
int_to_str(arg_index++, index_str);
|
||||
set_var_copy(vars, index_str, argv[i]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (options->type == INPUT_UNDEFINED)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue