Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 832907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:26:11+00:00 2026-05-15T04:26:11+00:00

I am writing a C program which is a frontend to a myriad tools.

  • 0

I am writing a C program which is a frontend to a myriad tools. This fronted will be launched like this:

my-frontend --action <AN ACTION>

As all the tools have the same prefix, let say for this example this prefix is “foo”. I want to concatenate “AN ACTION” to this prefix and exec this (if the tool exists).

I have written something but my implementation uses strcmp to test that “AN ACTION” is a valid action. Even if this works, I do not like it. So I am looking for a nicer solution that would do the same. The list of possibilities is pretty small (less than 10) and static (the list is “hardcoded”) but I am sure there is a more “C-ish” way to do this (using a struct or something like that). As I am not a C expert, I am asking for your help.

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:26:12+00:00Added an answer on May 15, 2026 at 4:26 am

    Take a look at getopt_long. Its part of the standard C library. For full documentation on how C programs are supposed to handle arguments in a standard UNIX way, check this.

    The above should give you the argument. But I see your problem is more than that. Dealing with multiple back-ends can be done like this:

    struct cmds {
            char *name;
            void (*cmd)(int argc, char **argv);
    };
    

    You could declare a cmd_table like this:

    struct cmds cmd_table[] = {
            { "backend1", backend1_exec },
            { "backend2", backend2_exec },
            ...
    };
    

    backend1_exec() and backend2_exec() are just functions which really do the grunt work for executing the real backend for you.

    Then in your main() function just loop over the entries comparing each entry in the cmd_table with the argument, and then calling the corresponding backend function like this:

    if (strcmp(cmdname, cmd_table[i].name)) {
            *(cmd_table[i].cmd)(argc, argv);
    }
    

    Yeah, there’s nothing wrong with using strcmp() if you just got 10 entries in the table. Its not a performance critical section of your program, so I wouldn’t worry too much about that. This method is a popular way of handling this, from what I have seen (that does not make it authoritative, but might help reassure you, that you’re not doing anything wrong).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.