Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Scanning for Options in C

Name: Anonymous 2010-10-29 4:12

Hi guys. I was wondering what the most common way to check for options, declared with '-' at the command line, is in C. For example: prog -a filename would run the program prog with the -a option. I assume it involves scanning with getopt. Thanks.

Name: Anonymous 2010-10-29 4:14

>>1

I forgot to mention, if it's not already apparent: the importance of scanning for the hyphen is to differentiate options from possible filenames, which is denoted above as filename.

Name: Anonymous 2010-10-29 4:36

>>1
You assume correctly. Now, what was the point of your thread again?

Name: Anonymous 2010-10-29 4:45

I wrote my own piece of code that has more sane structure for describing options, supports --long-args and can generate usage message automatically.

Name: Anonymous 2010-10-29 4:58

>>4
So you wrote another getopt clone that does the same thing as every other getopt clone in history?

Name: Anonymous 2010-10-29 5:09

>>6
His version has support for ``--long-args''.

Name: Anonymous 2010-10-29 5:44

>>6
-1 for quoting your own post.

Name: Anonymous 2010-10-29 6:09

>>5
No, it also supports --long-args and prints usage message, and usage for options is in the same struct as options themselves, and it also does pretty text-wrapping while printing usage.

Name: Anonymous 2010-10-29 6:17

Hi >>3-8. After doing some research and reading a good explanation authored by IBM located here:

http://www.ibm.com/developerworks/aix/library/au-unix-getopt.html

I got it working. Now, I have a new question that I can't seem to figure a clear way around: My program doesn't distinguish between options and filenames, when looking for filenames to scan with fopen(). For example:

prog -a testfile

will properly set the -a option and function as intended, but instead of then accessing testfile, it will first attempt to open a file named -a, which of course does not exist. What would be the best way to circumvent this? All commands are preceded with a minus symbol, and all commands set a bool in a global argument storage structure as in the IBM tutorial, i.e.

struct globalArgs_t {
    int noIndex;                /* -I option */
    char *langCode;             /* -l option */
    const char *outFileName;    /* -o option */
    FILE *outFile;
    int verbosity;              /* -v option */
    char **inputFiles;          /* input files */
    int numInputFiles;          /* # of input files */
}


Thanks for taking the time to read this post.

Name: Anonymous 2010-10-29 6:26

>>9
What if you want to process a file which starts with '-'? You are going to need some improvements there.
<

Name: Anonymous 2010-10-29 6:31

>>10

Would you mind linking to a page detailing this issue, or point me in the right direction? I am a *gulp* beginner programmer.

Name: Anonymous 2010-10-29 9:32

>>11
Example usage:
touch -- -a
rm ./-a

Don't change these.
Name: Email:
Entire Thread Thread List