Browse Source

Implement new PARAM's option 'value'. The subcommands can have the same values but different names. Fix issue #3.

git-svn-id: https://klish.googlecode.com/svn/trunk@140 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
2766b80398

+ 10 - 2
clish.xsd

@@ -223,8 +223,8 @@
 *           simply controls the conditional variable expansion for this 
 *           parameter.
 *
-* mode      - Define parameter mode. It can be common, switch or
-*           subcommand.
+* [mode]    - Parameter mode. It can be "common", "switch" or
+*           "subcommand".
 *
 * [prefix]  - defines the prefix for an optional parameter. A prefix
 *           with this value on the command line will signify the presence
@@ -237,6 +237,13 @@
 * [default] - defines a default value for a parameter. Any parameters
 *           at the end of command line which have default values need 
 *           not explicitly be entered.
+*
+* [value]   - defines the user's value for subcommand. If this option
+*           is defined the entered parameter will be compared to this
+*           value instead the "name" field. If this field is defined
+*           the mode of PARAM will be forced to "subcommand". The
+*           feature is implemented to support subcommands with the
+*           same names.
 ********************************************************
     -->
     <xs:simpleType name="bool_t">
@@ -264,6 +271,7 @@
         <xs:attribute name="prefix" type="xs:string" use="optional"/>
         <xs:attribute name="mode" type="param_mode_t" use="optional" default="common"/>
         <xs:attribute name="optional" type="bool_t" use="optional" default="false"/>
+        <xs:attribute name="value" type="xs:string" use="optional"/>
     </xs:complexType>
     <!--
 ********************************************************

+ 2 - 3
clish/command/command.c

@@ -37,7 +37,7 @@ clish_command_init(clish_command_t * this, const char *name, const char *text)
 	this->escape_chars = NULL;
 	this->args = NULL;
 	this->pview = NULL;
-	
+
 	/* CONFIG params */
 	this->cfg_op = CLISH_CONFIG_NONE;
 	this->priority = 0x7f00; /* medium priority by default */
@@ -206,7 +206,7 @@ void clish_command_help(const clish_command_t * this, const char *line)
 
 		param = clish_pargv__get_param(last, i);
 		if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(param))
-			name = clish_param__get_name(param);
+			name = clish_param__get_value(param);
 		else
 			name = clish_ptype__get_text(clish_param__get_ptype(param));
 		if (name)
@@ -499,4 +499,3 @@ unsigned short clish_command__get_seq_num(const clish_command_t * this)
 {
 	return this->seq_num;
 }
-

+ 2 - 1
clish/param.h

@@ -75,6 +75,8 @@ const unsigned clish_param__get_param_count(const clish_param_t * instance);
 clish_paramv_t *clish_param__get_paramv(clish_param_t * instance);
 void clish_param__set_optional(clish_param_t * instance, bool_t optional);
 bool_t clish_param__get_optional(const clish_param_t * instance);
+void clish_param__set_value(clish_param_t * instance, const char * value);
+char *clish_param__get_value(const clish_param_t * instance);
 
 /* paramv methods */
 clish_paramv_t *clish_paramv_new(void);
@@ -84,6 +86,5 @@ clish_param_t *clish_paramv__get_param(const clish_paramv_t * instance,
 				unsigned index);
 const unsigned clish_paramv__get_count(const clish_paramv_t * instance);
 
-
 #endif				/* _clish_param_h */
 /** @} clish_param */

+ 19 - 2
clish/param/param.c

@@ -28,6 +28,7 @@ clish_param_init(clish_param_t * this,
 	this->defval = NULL;
 	this->mode = CLISH_PARAM_COMMON;
 	this->optional = BOOL_FALSE;
+	this->value = NULL;
 
 	this->paramv = clish_paramv_new();
 }
@@ -44,6 +45,8 @@ static void clish_param_fini(clish_param_t * this)
 	this->name = NULL;
 	lub_string_free(this->text);
 	this->text = NULL;
+	lub_string_free(this->value);
+	this->value = NULL;
 
 	clish_paramv_delete(this->paramv);
 }
@@ -135,7 +138,7 @@ clish_param_mode_e clish_param__get_mode(const clish_param_t * this)
 char *clish_param_validate(const clish_param_t * this, const char *text)
 {
 	if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this)) {
-		if (lub_string_nocasecmp(clish_param__get_name(this), text))
+		if (lub_string_nocasecmp(clish_param__get_value(this), text))
 			return NULL;
 	}
 	return clish_ptype_translate(this->ptype, text);
@@ -162,7 +165,7 @@ void clish_param_help(const clish_param_t * this, size_t offset)
 	}
 
 	if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this))
-		name = this->name;
+		name = clish_param__get_value(this);
 	else
 		name = clish_ptype__get_text(this->ptype);
 
@@ -287,4 +290,18 @@ const unsigned clish_paramv__get_count(const clish_paramv_t * this)
 	return this->paramc;
 }
 
+/*--------------------------------------------------------- */
+void clish_param__set_value(clish_param_t * this, const char * value)
+{
+	assert(NULL == this->value);
+	this->value = lub_string_dup(value);
+}
+
+/*--------------------------------------------------------- */
+char *clish_param__get_value(const clish_param_t * this)
+{
+	if (this->value)
+		return this->value;
+	return this->name;
+}
 

+ 1 - 0
clish/param/param_dump.c

@@ -14,6 +14,7 @@ void clish_param_dump(const clish_param_t * this)
 	lub_dump_indent();
 	lub_dump_printf("name   : %s\n", this->name);
 	lub_dump_printf("text   : %s\n", this->text);
+	lub_dump_printf("value  : %s\n", this->value);
 	lub_dump_printf("ptype  : %s\n", clish_ptype__get_name(this->ptype));
 	lub_dump_printf("default: %s\n",
 			this->defval ? this->defval : "(null)");

+ 1 - 0
clish/param/private.h

@@ -15,6 +15,7 @@ struct clish_paramv_s {
 struct clish_param_s {
 	char *name;
 	char *text;
+	char *value;
 	clish_ptype_t *ptype;	/* The type of this parameter */
 	char *defval;		/* default value to use for this parameter */
 	clish_paramv_t *paramv;

+ 3 - 3
clish/pargv/pargv.c

@@ -132,11 +132,11 @@ clish_pargv_parse(clish_pargv_t * this,
 					if (CLISH_PARAM_SUBCOMMAND ==
 						clish_param__get_mode(cparam)) {
 						const char *pname =
-							clish_param__get_name(cparam);
+							clish_param__get_value(cparam);
 						if (!arg || (arg && 
 							(pname == lub_string_nocasestr(pname,
 							arg))))
-							clish_pargv_insert(last, 
+							clish_pargv_insert(last,
 								cparam, arg);
 					} else {
 						clish_pargv_insert(last,
@@ -147,7 +147,7 @@ clish_pargv_parse(clish_pargv_t * this,
 				if (CLISH_PARAM_SUBCOMMAND ==
 					clish_param__get_mode(param)) {
 					const char *pname =
-					    clish_param__get_name(param);
+					    clish_param__get_value(param);
 					if (!arg || (arg &&
 						(pname == lub_string_nocasestr(pname, arg))))
 						clish_pargv_insert(last, param, arg);

+ 2 - 2
clish/shell/shell_command_generator.c

@@ -97,8 +97,8 @@ static char *clish_shell_param_generator(clish_shell_t * this,
 				result = lub_string_dup(text);
 			} else if (CLISH_PARAM_SUBCOMMAND ==
 				clish_param__get_mode(param)) {
-				/* The subcommand is identified by it's name */
-				result = lub_string_dup(clish_param__get_name(param));
+				/* The subcommand is identified by it's value */
+				result = lub_string_dup(clish_param__get_value(param));
 			} else if (CLISH_PARAM_SWITCH ==
 				   clish_param__get_mode(param)) {
 				/* The switch has no completion string */

+ 10 - 0
clish/shell/shell_tinyxml_read.cpp

@@ -306,6 +306,7 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 		const char *defval = element->Attribute("default");
 		const char *mode = element->Attribute("mode");
 		const char *optional = element->Attribute("optional");
+		const char *value = element->Attribute("value");
 		clish_param_t *param;
 		clish_ptype_t *tmp = NULL;
 
@@ -376,12 +377,21 @@ process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
 			clish_param__set_optional(param, BOOL_TRUE);
 		else
 			clish_param__set_optional(param, BOOL_FALSE);
+
+		if (NULL != value) {
+			clish_param__set_value(param, value);
+			/* Force mode to subcommand */
+			clish_param__set_mode(param,
+				CLISH_PARAM_SUBCOMMAND);
+		}
+
 		if (cmd)
 			// add the parameter to the command
 			clish_command_insert_param(cmd, param);
 		if (p_param)
 			// add the parameter to the param
 			clish_param_insert_param(p_param, param);
+
 		process_children(shell, element, param);
 
 	}