Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to append the command after arugment? #97

Open
PFtyrant opened this issue Jun 30, 2024 · 3 comments
Open

how to append the command after arugment? #97

PFtyrant opened this issue Jun 30, 2024 · 3 comments

Comments

@PFtyrant
Copy link

Hi there!
stuck in the following situations...

  1. How do I append a command after an argument?
    example:
cmd looks like: ip igmp snooping vlan <VLAN-LIST> mrouter interfaces <INTERFACE-LIST>
  - ip
    - igmp
      - snooping
        - vlan
          - VLAN-LIST                     --> argument here
            - mrouter
              - interfaces
                - INTERFACES-LIST         --> argument here
  1. command and argument can't be at the same depth?
    example:
  •  VLAN
      -  <VLAN-LIST>                    --> this is arguments for VLAN
      -  default-vlan                   --> this is a sub command of VLAN command
    

I would appreciate any feedback! Thank you!

@PFtyrant PFtyrant changed the title how to append the command after arugments? how to append the command after arugment? Jun 30, 2024
@filka55533
Copy link

filka55533 commented Jun 30, 2024

Hello! You can dispatch it only with very hard validation callback on snooping command. I think, you have very hard command hierarchy. You can define vlan config mode, where you can store context within all required data about vlan, and declare commands, that would be enable on vlan config mode.

#define MODE_CONFIG_VLAN 0x02
struct vlan_context {
  // Add all fields that you need for vlan
};

int config_vlan(struct cli_def *cli, const char *command, char *argv[], int argc);

void initialize_commands(struct cli_def *cli) {
  struct cli_def *c;
  c = cli_register_command(cli, NULL, "vlan", config_vlan, PRIVILEGE_PRIVILEGED, MODE_CONFIG,
                           "Configure VLAN");

  c = cli_register_command(cli, c, "ip", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG, "ip help");
  c = cli_register_command(cli, c, "igmp", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG,
                           "igmp help");
  c = cli_register_command(cli, c, "snooping", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG,
                           "snooping help");
  c = cli_register_command(cli, c, "mrouter", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_VLAN,
                           "mrouter help");
  c = cli_register_command(cli, c, "interfaces", NULL, PRIVILEGE_PRIVILEGED, MODE_CONFIG_VLAN,
                           "interfaces help");
}

int config_vlan(struct cli_def *cli, const char *command, char *argv[], int argc) {
  static struct vlan_context ctx;
  // Your validation routine, if no argument should be use -- set default vlan id
  ...
  // If all is ok
  cli_set_context(cli, (void *)&ctx);
  cli_set_configmode(cli, MODE_CONFIG_VLAN, "vlan");
}

And than you can config it as

your_proj> enable 
your_proj# configure terminal
your_proj(config)# vlan 12
your_proj(config-vlan)# ip igmp snooping mrouter interfaces <INTERFACE-LIST>

For my opinion this would be much easier, than when you would write snooping command callback.

@PFtyrant
Copy link
Author

PFtyrant commented Jul 1, 2024

Thanks for your reply, @filka55533. it is a good opinion. Unfortunately, we don't want to modify the CLI command hierarchy. So, regarding question 2, is there any way to solve it?

@filka55533
Copy link

Just look on example with perimeter command on clitest.c . On optarg shape you can see function shape_transient_eval, where according to the shape (rectangle or triangle) should be setting new MODE_CONFIG_* mode. You can use same technique, where you should be enable your new mode (f.e. MODE_CONFIG_VLAN) and register other commands (mrouter and interfaces) only for this new mode like on the clitest.c file with side_1-4 arguments. But this solution would be harder to scale for new command if compare it with my previous solution, keep in mind it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants