user3698698
user3698698

Reputation: 21

Pass command line argument to a sub function

I have a C program main routine which calls heirarchically several levels of functions. Eg :

main -> MyFunc -> MySubFunc -> MySub2Func

and I have a condition in MySub2Func which needs to be checked against a command line argument. Eg:

if (myvar == argv[1]) 

Other than passing argv as a parameter to subfunction , is there any other way I could acheive this. (because I need to do this in several functions lying at different heirarchical levels)

Each of the sub-functions lie in different C files. My aim is to perform a debug by temporarily checking a particular local variable against a cmd line argument (and taking further actions accordingly) .. hence modifying the entire heirarchy is unfortunately not desirable for my purpose.


[update from comment]

sorry that I forgot to mention .. i am trying to perform a debug by temporarily checking a particular local variable against a cmd line argument (and taking further actions accordingly) .. hence modifying the entire heirarchy is unfortunately not desirable for my purpose ..

Upvotes: 1

Views: 5001

Answers (4)

alk
alk

Reputation: 70911

[for debugging]

Put this in a header included by all modules involved:

extern int g_argc;
extern char ** g_argv;

Define g_argc and g_argv globally in the main module

int g_argc = 0;
char ** g_argv = NULL;

Then in main() just do

int main(int argv, char ** argv)
{
  g_argc = argc;
  g_argv = argv;

and access g_argc and g_argv from the modules in question.

Upvotes: 0

merlin
merlin

Reputation: 136

As unwind said, the functions further down the chain should really not know anything about main's arguments. You should parse the command line arguments once setting the program's configuration. Additionally you should provide query functions for this configuration e.g.

/* --- config.c */

typedef struct {
   int debug_enabled;
} config_t;

static config_t configuration;

int debug_enabled() {
    return configuration->debug_enabled;
}

void initialize_config() {
    /* set default parameters */
}

int set_config_from_cmd(int argc, char** argv){
    /* parse CMD parameters and set config */
}


/* --- main.c */
int main(int argc, char** argv)
{
    initialize_config();
    if ( set_config_from_cmd(argc, argv) == -1 ) {
        exit(-1);
    }

    /* do stuff, call functions */
}


int myfunction() {
    if ( debug_enabled() ) {
        printf("debug!");
    }
    /* do stuff */
}

Notice that I put the configuration related stuff into a separate file in order to hide the internals of the configuration structure. Only the interface e.g. debug_enabled() etc is exposed.

Upvotes: 0

René Nyffenegger
René Nyffenegger

Reputation: 40499

You might use global variables that are set in the main() function:

int    g_argc;
char **g_argv;

int main(int argc, char **argv) {
    g_argc = argc;
    g_argv = argv;

    MyFunc();

}

...

void MyFunc() {
  MySubFunc();
}

...

void MySubFunc() {
  MySub2Func();
}

...

void MySub2Func() {
   if (myvar == g_argv[1]) {
     do_the_thing();
   }
}

Upvotes: 2

unwind
unwind

Reputation: 399803

The common approach is to "decouple" the two; the functions further down the call tree really shouldn't care or know about main()'s arguments, i.e. the command argument vector itself.

Instead, it should be abstracted into application-specific options, which are passed from main(), which parses the options out of the command line arguments, down to all application-specific functions that need them.

Upvotes: 2

Related Questions