Let config(1) work on multiple values at once

This commit is contained in:
Pierre Pronchery 2014-05-17 17:34:28 +02:00
parent 4f2440f4c1
commit 2c33aeb109
2 changed files with 20 additions and 14 deletions

View File

@ -52,7 +52,7 @@
<arg choice="plain"><option>-f</option> <arg choice="plain"><option>-f</option>
<replaceable>filename</replaceable></arg> <replaceable>filename</replaceable></arg>
<arg choice="opt"><option>-v</option></arg> <arg choice="opt"><option>-v</option></arg>
<arg choice="plain"> <arg choice="plain" rep="repeat">
<arg choice="opt"><replaceable>section.</replaceable></arg> <arg choice="opt"><replaceable>section.</replaceable></arg>
<arg choice="plain"><replaceable>variable</replaceable></arg> <arg choice="plain"><replaceable>variable</replaceable></arg>
</arg> </arg>
@ -63,7 +63,7 @@
<arg choice="plain"><option>-f</option> <arg choice="plain"><option>-f</option>
<replaceable>filename</replaceable></arg> <replaceable>filename</replaceable></arg>
<arg choice="opt"><option>-v</option></arg> <arg choice="opt"><option>-v</option></arg>
<arg choice="plain"> <arg choice="plain" rep="repeat">
<arg choice="opt"><replaceable>section.</replaceable></arg> <arg choice="opt"><replaceable>section.</replaceable></arg>
<arg choice="plain"><replaceable>variable</replaceable></arg> <arg choice="plain"><replaceable>variable</replaceable></arg>
<arg choice="opt"><replaceable>=value</replaceable></arg> <arg choice="opt"><replaceable>=value</replaceable></arg>

View File

@ -84,8 +84,8 @@ static int _error(char const * progname, int ret)
/* usage */ /* usage */
static int _usage(void) static int _usage(void)
{ {
fputs("Usage: config -f filename [-v] [section.]key\n" fputs("Usage: config -f filename [-v] [section.]key...\n"
" config -w -f filename [-v] [section.]key[=value]\n", stderr); " config -w -f filename [-v] [section.]key[=value]...\n", stderr);
return 1; return 1;
} }
@ -95,10 +95,12 @@ static int _usage(void)
/* main */ /* main */
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
int ret = 0;
int o; int o;
char const * filename = NULL; char const * filename = NULL;
int verbose = 0; int verbose = 0;
int write = 0; int write = 0;
int i;
char * section; char * section;
char * key; char * key;
char * value = NULL; char * value = NULL;
@ -118,17 +120,21 @@ int main(int argc, char * argv[])
default: default:
return _usage(); return _usage();
} }
if(filename == NULL || optind + 1 != argc) if(filename == NULL || optind == argc)
return _usage(); return _usage();
section = argv[optind]; for(i = optind; i < argc; i++)
if((key = strchr(section, '.')) == NULL)
{ {
key = section; section = argv[i];
section = NULL; if((key = strchr(section, '.')) == NULL)
{
key = section;
section = NULL;
}
else
*(key++) = '\0';
if(write && (value = strchr(key, '=')) != NULL)
*(value++) = '\0';
ret |= _config(verbose, filename, section, key, value);
} }
else return (ret == 0) ? 0 : 2;
*(key++) = '\0';
if(write && (value = strchr(key, '=')) != NULL)
*(value++) = '\0';
return (_config(verbose, filename, section, key, value) == 0) ? 0 : 2;
} }