2011-04-23 16:42:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Your File System Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the command line parsing library
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <afsconfig.h>
|
|
|
|
#include <afs/param.h>
|
|
|
|
|
|
|
|
#include <roken.h>
|
|
|
|
|
|
|
|
#include <afs/cmd.h>
|
|
|
|
|
2012-05-07 21:52:16 +01:00
|
|
|
#include <tests/tap/basic.h>
|
2011-04-23 16:42:54 +01:00
|
|
|
|
2011-10-08 23:33:37 +01:00
|
|
|
enum cmdOptions {
|
|
|
|
copt_flag = 0,
|
|
|
|
copt_first,
|
|
|
|
copt_second,
|
|
|
|
copt_sugar,
|
|
|
|
copt_fourth,
|
|
|
|
copt_fifth,
|
|
|
|
copt_perhaps,
|
|
|
|
copt_sanity
|
|
|
|
};
|
2011-05-23 22:51:59 +01:00
|
|
|
|
2011-04-23 16:42:54 +01:00
|
|
|
static int
|
|
|
|
testproc(struct cmd_syndesc *as, void *rock)
|
|
|
|
{
|
2011-10-08 23:33:37 +01:00
|
|
|
is_string("foo", as->parms[copt_first].items->data,
|
2011-04-23 16:42:54 +01:00
|
|
|
"first option matches");
|
2011-10-08 23:33:37 +01:00
|
|
|
is_string("bar", as->parms[copt_second].items->data,
|
2011-04-23 16:42:54 +01:00
|
|
|
"second option matches");
|
2011-10-08 23:33:37 +01:00
|
|
|
ok(as->parms[copt_flag].items != NULL,
|
2011-04-23 16:42:54 +01:00
|
|
|
"flag is set");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-10 16:51:01 +01:00
|
|
|
static void
|
|
|
|
checkList(struct cmd_item *list, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *el;
|
|
|
|
|
|
|
|
va_start(ap, list);
|
|
|
|
el = va_arg(ap, char *);
|
|
|
|
while (el != NULL && list != NULL) {
|
|
|
|
is_string(el, list->data, "list element matches");
|
|
|
|
list = list->next;
|
|
|
|
el = va_arg(ap, char *);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (el == NULL && list == NULL) {
|
|
|
|
ok(1, "List has correct number of elements");
|
|
|
|
} else if (el == NULL) {
|
|
|
|
ok(0, "List has too many elements");
|
|
|
|
} else if (list == NULL) {
|
|
|
|
ok(0, "List has too few elements");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 16:42:54 +01:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *tv[100];
|
|
|
|
struct cmd_syndesc *opts;
|
2011-04-19 11:47:08 +01:00
|
|
|
struct cmd_syndesc *retopts;
|
2012-05-10 16:51:01 +01:00
|
|
|
struct cmd_item *list;
|
2011-04-23 16:42:54 +01:00
|
|
|
int code;
|
|
|
|
int tc;
|
2011-04-19 11:41:54 +01:00
|
|
|
int retval;
|
cmd: Add support for pulling options from files
With this change, we gain the ability to set our command line options
from krb5.conf configuration files. This is only available for tools
which are implemented using the new cmd_OptionAs accessor methods.
Callers should load their configuration file using
cmd_OpenConfigFile("/path/to/config/file.conf");
(an addition to libauth to return a path to a system wide
configuration file will be forthcoming)
and then set their command name (for example, "fileserver", "afsd",
"vos" and so on) using
cmd_SetCommandName("mycommand");
The accessor functions will then populate their return values with
either:
a) The command line options, if specified
b) The contents of the tag matching the option name, in the
[command_subcommand] section of the configuration file, if it
exists
c) The contents of the same tag, in the [command] section of the
configuration file, if it that exists.
d) The contents of the same tag in the [defaults] section of the
configuration file.
Callers can also gain access to the entire configuration file by
calling cmd_RawFile, or to just the section corresponding to their
command line by calling cmd_RawSection. Note that when using the file
directly, it is up to callers to preserve consistency by implementing
similar inheritance rules as the above.
Change-Id: Ic501ab296af3638f961486869af79c9ce47b77b8
Reviewed-on: http://gerrit.openafs.org/7135
Reviewed-by: Derrick Brashear <shadow@dementix.org>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
2011-10-09 01:20:57 +01:00
|
|
|
char *path;
|
2011-04-24 20:29:37 +01:00
|
|
|
char *retstring = NULL;
|
2011-04-23 16:42:54 +01:00
|
|
|
|
cmd: Add support for pulling options from files
With this change, we gain the ability to set our command line options
from krb5.conf configuration files. This is only available for tools
which are implemented using the new cmd_OptionAs accessor methods.
Callers should load their configuration file using
cmd_OpenConfigFile("/path/to/config/file.conf");
(an addition to libauth to return a path to a system wide
configuration file will be forthcoming)
and then set their command name (for example, "fileserver", "afsd",
"vos" and so on) using
cmd_SetCommandName("mycommand");
The accessor functions will then populate their return values with
either:
a) The command line options, if specified
b) The contents of the tag matching the option name, in the
[command_subcommand] section of the configuration file, if it
exists
c) The contents of the same tag, in the [command] section of the
configuration file, if it that exists.
d) The contents of the same tag in the [defaults] section of the
configuration file.
Callers can also gain access to the entire configuration file by
calling cmd_RawFile, or to just the section corresponding to their
command line by calling cmd_RawSection. Note that when using the file
directly, it is up to callers to preserve consistency by implementing
similar inheritance rules as the above.
Change-Id: Ic501ab296af3638f961486869af79c9ce47b77b8
Reviewed-on: http://gerrit.openafs.org/7135
Reviewed-by: Derrick Brashear <shadow@dementix.org>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
2011-10-09 01:20:57 +01:00
|
|
|
plan(109);
|
2011-04-23 16:42:54 +01:00
|
|
|
|
|
|
|
initialize_CMD_error_table();
|
|
|
|
|
|
|
|
opts = cmd_CreateSyntax(NULL, testproc, NULL, NULL);
|
2011-05-23 23:04:20 +01:00
|
|
|
cmd_AddParm(opts, "-flag", CMD_FLAG, CMD_OPTIONAL, "a flag");
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_AddParm(opts, "-first", CMD_SINGLE, CMD_REQUIRED, "first option");
|
|
|
|
cmd_AddParm(opts, "-second", CMD_LIST, CMD_OPTIONAL, "second option");
|
|
|
|
|
|
|
|
/* A simple command line */
|
|
|
|
code = cmd_ParseLine("-first foo -second bar -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(0, code, "dispatching simple comamnd line succeeds");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "parsing simple command line succeeds");
|
2011-10-08 23:33:37 +01:00
|
|
|
is_string("foo", retopts->parms[copt_first].items->data,
|
|
|
|
" ... 1st option matches");
|
|
|
|
is_string("bar", retopts->parms[copt_second].items->data,
|
|
|
|
" ... 2nd option matches");
|
|
|
|
ok(retopts->parms[copt_flag].items != NULL, " ... 3rd option matches");
|
2011-04-19 11:47:08 +01:00
|
|
|
cmd_FreeOptions(&retopts);
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* unknown switch */
|
|
|
|
code = cmd_ParseLine("-first foo -second bar -third -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "invalid options fail as expected");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* missing parameter */
|
|
|
|
code = cmd_ParseLine("-first foo -second -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(CMD_TOOFEW, code, "missing parameters fail as expected");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_TOOFEW, code, "and still fail with cmd_Parse");
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* missing option */
|
|
|
|
code = cmd_ParseLine("-second bar -third -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "missing options fail as expected");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "and still fail with cmd_Parse");
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
code = cmd_ParseLine("-first foo baz -second bar -third -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(CMD_NOTLIST, code, "too many parameters fails as expected");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_NOTLIST, code, "and still fail with cmd_Parse");
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* Positional parameters */
|
|
|
|
code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(0, code, "dispatching positional parameters succeeds");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "and works with cmd_Parse");
|
|
|
|
cmd_FreeOptions(&retopts);
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* Abbreviations */
|
|
|
|
code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(0, code, "dispatching abbreviations succeeds");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "and works with cmd_Parse");
|
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* Ambiguous */
|
|
|
|
code = cmd_ParseLine("-f foo -s bar -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");
|
2011-04-23 16:42:54 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
2011-05-30 20:02:31 +01:00
|
|
|
/* Check that paramaters with abbreviation disabled don't make things
|
|
|
|
* ambiguous */
|
2011-10-08 23:33:37 +01:00
|
|
|
cmd_AddParmAtOffset(opts, copt_sugar, "-sugar", CMD_SINGLE,
|
|
|
|
CMD_OPTIONAL | CMD_NOABBRV, "sugar with that");
|
2011-05-30 20:02:31 +01:00
|
|
|
code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(0, code, "disabling specific abbreviations succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "and works with cmd_Parse into the bargain");
|
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
2011-04-18 08:25:55 +01:00
|
|
|
/* Disable positional commands */
|
|
|
|
cmd_DisablePositionalCommands();
|
|
|
|
code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
2011-04-19 11:47:08 +01:00
|
|
|
is_int(CMD_NOTLIST, code, "positional parameters can be disabled");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_NOTLIST, code, "and fail with cmd_Parse too");
|
2011-04-18 08:25:55 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
2011-04-18 08:31:42 +01:00
|
|
|
/* Disable abbreviations */
|
|
|
|
cmd_DisableAbbreviations();
|
|
|
|
code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Dispatch(tc, tv);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "dispatching abbreviations succeeds");
|
2011-04-19 11:47:08 +01:00
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too");
|
|
|
|
|
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* Try the new cmd_Parse function with something different*/
|
|
|
|
code = cmd_ParseLine("-first one -second two -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "Parsing with cmd_Parse works");
|
2011-10-08 23:33:37 +01:00
|
|
|
is_string("one", retopts->parms[copt_first].items->data,
|
|
|
|
" ... 1st option matches");
|
|
|
|
is_string("two", retopts->parms[copt_second].items->data,
|
|
|
|
" ... 2nd option matches");
|
cmd: Add support for pulling options from files
With this change, we gain the ability to set our command line options
from krb5.conf configuration files. This is only available for tools
which are implemented using the new cmd_OptionAs accessor methods.
Callers should load their configuration file using
cmd_OpenConfigFile("/path/to/config/file.conf");
(an addition to libauth to return a path to a system wide
configuration file will be forthcoming)
and then set their command name (for example, "fileserver", "afsd",
"vos" and so on) using
cmd_SetCommandName("mycommand");
The accessor functions will then populate their return values with
either:
a) The command line options, if specified
b) The contents of the tag matching the option name, in the
[command_subcommand] section of the configuration file, if it
exists
c) The contents of the same tag, in the [command] section of the
configuration file, if it that exists.
d) The contents of the same tag in the [defaults] section of the
configuration file.
Callers can also gain access to the entire configuration file by
calling cmd_RawFile, or to just the section corresponding to their
command line by calling cmd_RawSection. Note that when using the file
directly, it is up to callers to preserve consistency by implementing
similar inheritance rules as the above.
Change-Id: Ic501ab296af3638f961486869af79c9ce47b77b8
Reviewed-on: http://gerrit.openafs.org/7135
Reviewed-by: Derrick Brashear <shadow@dementix.org>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
2011-10-09 01:20:57 +01:00
|
|
|
ok(retopts->parms[copt_flag].items != NULL,
|
|
|
|
" ... 3rd option matches");
|
2011-04-19 11:47:08 +01:00
|
|
|
|
2011-04-19 09:01:25 +01:00
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
/* Try adding a couple of parameters at specific positions */
|
2011-10-08 23:33:37 +01:00
|
|
|
cmd_AddParmAtOffset(opts, copt_fifth, "-fifth", CMD_SINGLE, CMD_OPTIONAL,
|
2011-05-30 19:23:49 +01:00
|
|
|
"fifth option");
|
2011-10-08 23:33:37 +01:00
|
|
|
cmd_AddParmAtOffset(opts, copt_fourth, "-fourth", CMD_SINGLE, CMD_OPTIONAL,
|
2011-05-30 19:23:49 +01:00
|
|
|
"fourth option" );
|
2011-04-19 09:01:25 +01:00
|
|
|
code = cmd_ParseLine("-first a -fourth b -fifth c", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "parsing our new options succeeds");
|
2011-10-08 23:33:37 +01:00
|
|
|
is_string("b", retopts->parms[copt_fourth].items->data,
|
|
|
|
" Fourth option in right place");
|
|
|
|
is_string("c", retopts->parms[copt_fifth].items->data,
|
|
|
|
" Fifth option in right place");
|
2011-04-19 11:47:08 +01:00
|
|
|
cmd_FreeOptions(&retopts);
|
2011-04-18 08:31:42 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
2011-04-19 11:41:54 +01:00
|
|
|
/* Check Accessors */
|
|
|
|
code = cmd_ParseLine("-first 1 -second second -flag", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_OptionAsInt(retopts, copt_first, &retval);
|
2011-04-19 11:41:54 +01:00
|
|
|
is_int(0, code, "cmd_OptionsAsInt succeeds");
|
|
|
|
is_int(1, retval, " ... and returns correct value");
|
2012-05-10 16:51:01 +01:00
|
|
|
ok(cmd_OptionPresent(retopts, copt_first),
|
|
|
|
" ... and is marked as present");
|
2011-04-19 11:41:54 +01:00
|
|
|
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_OptionAsString(retopts, copt_second, &retstring);
|
2011-04-19 11:41:54 +01:00
|
|
|
is_int(0, code, "cmd_OptionsAsString succeeds");
|
|
|
|
is_string("second", retstring, " ... and returns correct value");
|
|
|
|
free(retstring);
|
|
|
|
retstring = NULL;
|
2012-05-10 16:51:01 +01:00
|
|
|
ok(cmd_OptionPresent(retopts, copt_second),
|
|
|
|
" ... and is marked as present");
|
2011-04-19 11:41:54 +01:00
|
|
|
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_OptionAsFlag(retopts, copt_flag, &retval);
|
2011-04-19 11:41:54 +01:00
|
|
|
is_int(0, code, "cmd_OptionsAsFlag succeeds");
|
|
|
|
ok(retval, " ... and flag is correct");
|
2012-05-10 16:51:01 +01:00
|
|
|
ok(cmd_OptionPresent(retopts, copt_flag),
|
|
|
|
" ... and is marked as present");
|
|
|
|
|
|
|
|
ok(!cmd_OptionPresent(retopts, copt_sugar),
|
|
|
|
"Missing option is marked as such");
|
2011-04-19 11:41:54 +01:00
|
|
|
|
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
2011-04-19 12:20:14 +01:00
|
|
|
/* Add an alias */
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_AddParmAlias(opts, copt_second, "-twa");
|
2011-04-19 12:20:14 +01:00
|
|
|
is_int(0, code, "cmd_AddParmAlias succeeds");
|
|
|
|
|
|
|
|
code = cmd_ParseLine("-first 1 -twa tup", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "cmd_Parse succeeds for alias");
|
2011-10-08 23:33:37 +01:00
|
|
|
cmd_OptionAsString(retopts, copt_second, &retstring);
|
2011-04-19 12:20:14 +01:00
|
|
|
is_string("tup", retstring, " ... and we have the correct value");
|
|
|
|
free(retstring);
|
|
|
|
retstring = NULL;
|
|
|
|
|
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
cmd_FreeArgv(tv);
|
2011-04-19 11:41:54 +01:00
|
|
|
|
2011-04-19 19:37:37 +01:00
|
|
|
/* Add something that can be a flag or a value, and put something after
|
|
|
|
* it so we can check for parse problems*/
|
|
|
|
cmd_AddParm(opts, "-perhaps", CMD_SINGLE_OR_FLAG, CMD_OPTIONAL,
|
|
|
|
"what am I");
|
|
|
|
cmd_AddParm(opts, "-sanity", CMD_SINGLE, CMD_OPTIONAL, "sanity check");
|
|
|
|
|
|
|
|
/* Try using as an option */
|
|
|
|
|
|
|
|
code = cmd_ParseLine("-first 1 -perhaps 2 -sanity 3", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "cmd_Parse succeeds for option-as-flag as opt");
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
|
2011-04-19 19:37:37 +01:00
|
|
|
is_int(0, code, "cmd_OptionAsInt succeeds");
|
|
|
|
is_int(2, retval, " ... and we have the correct value");
|
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
|
|
|
/* And now, as a flag */
|
|
|
|
|
|
|
|
code = cmd_ParseLine("-first 1 -perhaps -sanity 3", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "cmd_Parse succeeds for option-as-flag as flag");
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
|
2011-04-19 19:37:37 +01:00
|
|
|
is_int(CMD_MISSING, code, " ... pulling out a value fails as expected");
|
2011-10-08 23:33:37 +01:00
|
|
|
cmd_OptionAsFlag(retopts, copt_perhaps, &retval);
|
2011-04-19 19:37:37 +01:00
|
|
|
ok(retval, " ... but parsing as a flag works");
|
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
2011-04-19 19:59:13 +01:00
|
|
|
/* Check that we can produce help output */
|
|
|
|
code = cmd_ParseLine("-help", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(CMD_USAGE, code, "cmd_Parse returns usage error with help output");
|
|
|
|
ok(retopts == NULL, " ... and options is empty");
|
2011-04-20 22:26:14 +01:00
|
|
|
|
|
|
|
/* Check splitting with '=' */
|
|
|
|
|
|
|
|
code = cmd_ParseLine("-first 1 -perhaps=6 -sanity=3", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "cmd_Parse succeeds for items split with '='");
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
|
2011-04-20 22:26:14 +01:00
|
|
|
is_int(0, code, "cmd_OptionAsInt succeeds");
|
|
|
|
is_int(6, retval, " ... and we have the correct value once");
|
2011-10-08 23:33:37 +01:00
|
|
|
code = cmd_OptionAsInt(retopts, copt_sanity, &retval);
|
2011-04-20 22:26:14 +01:00
|
|
|
is_int(0, code, "cmd_OptionAsInt succeeds");
|
|
|
|
is_int(3, retval, " ... and we have the correct value twice");
|
|
|
|
cmd_FreeOptions(&retopts);
|
2011-04-19 19:59:13 +01:00
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
2012-05-10 16:51:01 +01:00
|
|
|
/* Check list behaviour */
|
|
|
|
code = cmd_ParseLine("-first 1 -second one two three", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "cmd_Parse succeeds for a list");
|
|
|
|
code = cmd_OptionAsList(retopts, copt_second, &list);
|
|
|
|
is_int(0, code, "cmd_OptionAsList succeeds");
|
|
|
|
checkList(list, "one", "two", "three", NULL);
|
|
|
|
cmd_FreeOptions(&retopts);
|
|
|
|
cmd_FreeArgv(tv);
|
|
|
|
|
cmd: Add support for pulling options from files
With this change, we gain the ability to set our command line options
from krb5.conf configuration files. This is only available for tools
which are implemented using the new cmd_OptionAs accessor methods.
Callers should load their configuration file using
cmd_OpenConfigFile("/path/to/config/file.conf");
(an addition to libauth to return a path to a system wide
configuration file will be forthcoming)
and then set their command name (for example, "fileserver", "afsd",
"vos" and so on) using
cmd_SetCommandName("mycommand");
The accessor functions will then populate their return values with
either:
a) The command line options, if specified
b) The contents of the tag matching the option name, in the
[command_subcommand] section of the configuration file, if it
exists
c) The contents of the same tag, in the [command] section of the
configuration file, if it that exists.
d) The contents of the same tag in the [defaults] section of the
configuration file.
Callers can also gain access to the entire configuration file by
calling cmd_RawFile, or to just the section corresponding to their
command line by calling cmd_RawSection. Note that when using the file
directly, it is up to callers to preserve consistency by implementing
similar inheritance rules as the above.
Change-Id: Ic501ab296af3638f961486869af79c9ce47b77b8
Reviewed-on: http://gerrit.openafs.org/7135
Reviewed-by: Derrick Brashear <shadow@dementix.org>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
2011-10-09 01:20:57 +01:00
|
|
|
/* Now, try adding a configuration file into the mix */
|
|
|
|
if (getenv("SOURCE") == NULL)
|
|
|
|
path = strdup("test1.conf");
|
|
|
|
else
|
|
|
|
asprintf(&path, "%s/command/test1.conf", getenv("SOURCE"));
|
|
|
|
|
|
|
|
cmd_SetCommandName("test");
|
|
|
|
code = cmd_OpenConfigFile(path);
|
|
|
|
is_int(0, code, "cmd_OpenConfigFile succeeds");
|
|
|
|
|
|
|
|
code = cmd_ParseLine("-first 1", tv, &tc, 100);
|
|
|
|
is_int(0, code, "cmd_ParseLine succeeds");
|
|
|
|
code = cmd_Parse(tc, tv, &retopts);
|
|
|
|
is_int(0, code, "cmd_Parse succeeds when we have a config file");
|
|
|
|
code = cmd_OptionAsInt(retopts, copt_perhaps, &retval);
|
|
|
|
is_int(0, code, "cmd_OptionsAsInt succeeds");
|
|
|
|
is_int(10, retval, " ... and we have the correct value for perhaps");
|
|
|
|
code = cmd_OptionAsString(retopts, copt_sanity, &retstring);
|
|
|
|
is_int(0, code, "cmd_OptionAsString succeeds");
|
|
|
|
is_string("testing", retstring,
|
|
|
|
" ... and we have the correct value for sanity");
|
|
|
|
|
|
|
|
/* Check breaking up a list of options */
|
|
|
|
code = cmd_OptionAsList(retopts, copt_second, &list);
|
|
|
|
is_int(0, code, "cmd_OptionAsList succeeds");
|
|
|
|
checkList(list, "one", "two", "three", "four", NULL);
|
|
|
|
|
2011-04-23 16:42:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2011-04-18 08:25:55 +01:00
|
|
|
|