FAQ
I am working on a program that has users enter commands in a command line
interface. Is a giant switch / case statement the only way to structure a
program like this? There will probably be several hundred commands, some
with several arguments.

Thanks,

Chris Schooley

Search Discussions

  • Rarul at Aug 14, 2001 at 5:48 pm
    Use GetOpt::Long or GetOpt::Std modules for processing command-line options.

    - Rex

    -----Original Message-----
    From: Schooley, Chris
    Sent: Tuesday, August 14, 2001 1:47 PM
    To: '[email protected]'
    Subject: Command line interface


    I am working on a program that has users enter commands in a command line
    interface. Is a giant switch / case statement the only way to structure a
    program like this? There will probably be several hundred commands, some
    with several arguments.

    Thanks,

    Chris Schooley

    --
    To unsubscribe, e-mail: [email protected]
    For additional commands, e-mail: [email protected]
  • Schooley, Chris at Aug 14, 2001 at 5:54 pm
    Forgot to send to the list...

    -----Original Message-----
    From: Schooley, Chris
    Sent: Tuesday, August 14, 2001 10:53 AM
    To: '[email protected]'
    Subject: RE: Command line interface


    Sorry, I should clarify - the program itself is like shell, has its own
    commands, etc. It is intended to simulate the command line interface found
    on various Cisco devices.

    -----Original Message-----
    From: [email protected]
    Sent: Tuesday, August 14, 2001 10:47 AM
    To: [email protected]
    Cc: [email protected]
    Subject: RE: Command line interface


    Use GetOpt::Long or GetOpt::Std modules for processing command-line options.

    - Rex

    -----Original Message-----
    From: Schooley, Chris
    Sent: Tuesday, August 14, 2001 1:47 PM
    To: '[email protected]'
    Subject: Command line interface


    I am working on a program that has users enter commands in a command line
    interface. Is a giant switch / case statement the only way to structure a
    program like this? There will probably be several hundred commands, some
    with several arguments.

    Thanks,

    Chris Schooley

    --
    To unsubscribe, e-mail: [email protected]
    For additional commands, e-mail: [email protected]
  • Michael Fowler at Aug 14, 2001 at 6:19 pm

    On Tue, Aug 14, 2001 at 10:54:05AM -0700, Schooley, Chris wrote:
    Sorry, I should clarify - the program itself is like shell, has its own
    commands, etc. It is intended to simulate the command line interface found
    on various Cisco devices.
    Based on this, I'm going to assume you have a set of commands that you're
    checking thusly:

    if ($command eq 'foo') {
    do_foo();
    } elsif ($command eq 'bar') {
    do_bar();
    } ...


    What I typically do for problems like these is use a hash of hashes:

    %commands = (
    foo => {
    sub => \&do_foo,
    },

    bar => {
    sub => \&do_bar,
    },
    );

    Then you simply do a lookup:

    if ($commands{$command}) {
    $commands{$command}{'sub'}->();
    } else {
    die("Unknown command name \"$command\".\n");
    }


    I use a hash of hashes, instead of a hash of name => subroutine pairs, so
    that I can include other meta-data along with the command. For instance:

    %commands = (
    foo => {
    sub => \&do_foo,
    arguments => 2,
    usage => '<arg1> <arg2>',
    },

    bar => {
    sub => \&do_bar,
    arguments => [0, 1],
    usage => '[<optional arg1>]',
    },
    );

    With this, you can do things like check the number of arguments specified
    for the command, and if there aren't enough, or there are too many, print out
    a usage message.


    Michael
    --
    Administrator www.shoebox.net
    Programmer, System Administrator www.gallanttech.com
    --
  • Schooley, Chris at Aug 14, 2001 at 6:26 pm
    Right on! This is a much better way than I was considering.

    "There is more than one way to do it"

    Thanks,
    Chris

    -----Original Message-----
    From: Michael Fowler
    Sent: Tuesday, August 14, 2001 11:20 AM
    To: Schooley, Chris
    Cc: '[email protected]'
    Subject: Re: Command line interface

    On Tue, Aug 14, 2001 at 10:54:05AM -0700, Schooley, Chris wrote:
    Sorry, I should clarify - the program itself is like shell, has its own
    commands, etc. It is intended to simulate the command line interface found
    on various Cisco devices.
    Based on this, I'm going to assume you have a set of commands that you're
    checking thusly:

    if ($command eq 'foo') {
    do_foo();
    } elsif ($command eq 'bar') {
    do_bar();
    } ...


    What I typically do for problems like these is use a hash of hashes:

    %commands = (
    foo => {
    sub => \&do_foo,
    },

    bar => {
    sub => \&do_bar,
    },
    );

    Then you simply do a lookup:

    if ($commands{$command}) {
    $commands{$command}{'sub'}->();
    } else {
    die("Unknown command name \"$command\".\n");
    }


    I use a hash of hashes, instead of a hash of name => subroutine pairs, so
    that I can include other meta-data along with the command. For instance:

    %commands = (
    foo => {
    sub => \&do_foo,
    arguments => 2,
    usage => '<arg1> <arg2>',
    },

    bar => {
    sub => \&do_bar,
    arguments => [0, 1],
    usage => '[<optional arg1>]',
    },
    );

    With this, you can do things like check the number of arguments specified
    for the command, and if there aren't enough, or there are too many, print
    out
    a usage message.


    Michael
    --
    Administrator www.shoebox.net
    Programmer, System Administrator www.gallanttech.com
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedAug 14, '01 at 5:47p
activeAug 14, '01 at 6:26p
posts5
users3
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase