dparrish / libcli

Libcli provides a shared library for including a Cisco-like command-line interface into other software. It's a telnet interface which supports command-line editing, history, authentication and callbacks for a user-definable function tree.
https://dparrish.com/link/libcli
GNU Lesser General Public License v2.1
289 stars 143 forks source link

Argument number from list and show availible numers list on help info #96

Closed filka55533 closed 1 week ago

filka55533 commented 1 week ago

Hello, i have no idea how to create configure function, for example selection interface FastEthernet where available argument is number from number list. Next example:

(config)# interface FastEthernet 
<port_type_list>  
(config)# interface FastEthernet ?
    <port_type_list>    Port list in 1/1-15
(config)# interface FastEthernet ?
interface ( <port_type> [ <plist> ] ) 

Thank you in advance

RobSanders commented 1 week ago

Ivan, There are a 3 missing pieces I think, just by going with what you have above. Two of them are 'optional'.

1) The required callback . The callback routine can use the cli_get_optarg_value() call to find out what the user actually entered - for example user_val = cli_get_optarg_value(cli, "port_type_list", NULL); This will return a char* to the string entered by the user. You'll have to convert to a number yourself.

2) An optional 'completor' function. This is a routine you need to write that can return a selection of possible values, base on what the user actually entered. For instance, if your values are 1 to 15, and the user enters '1', then possible values are 1, 10, 11,12,13,14, and 15.

3) An optional 'validator' function. This is a routine you would write that ensures that the user entered value is actually valid. You'll get passed the string and have to convert to an integer yourself.

Look at the 'clitest.c' program in the source code. It doesn't have an callback along the lines of what you are doing, but it does show the completor/validator/callback for the 'color' or 'shape' optargs on the 'perimeter' command.