Note that there are some explanatory texts on larger screens.

plurals
  1. POC/C++ getopt optstring syntax
    primarykey
    data
    text
    <p>Using the <code>getopt</code> function included in unistd.h in C++, is there a way to structure the optstring such that...</p> <p><code>[-a] [-f "reg_expr"] out_file1 [[-f "reg_expr"] out_file2 ...]</code> is possible?</p> <p>This is a homework assignment, but the emphasis is not on this specific subtask.</p> <p>In my head I would like to specify the following logic:</p> <p>(a argument), (infinitely many f arguments with 2 required (sub)arguments),... (infinitely many generic arguments)</p> <p>Perhaps my understanding of the <code>getopt</code> function is fundamentally flawed. I also saw a <code>getopt_long</code>. Perhaps that is what I'm missing.</p> <p>I originally drafted this, which worked, but I came across the <code>getopt</code> function and thought it might do a better job.</p> <pre><code>int outFileFlags; int outFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; int i = 1; while (i &lt; argc){ if (i == 1 &amp;&amp; strcmp( argv[i], "-a") == 0){ cout &lt;&lt; "append flag set" &lt;&lt; endl; outFileFlags = O_RDWR | O_APPEND; i++; continue; } else { outFileFlags = O_TRUNC | O_RDWR | O_CREAT; } if (strcmp( argv[i], "-f") == 0 &amp;&amp; i+2 &lt;= argc){ cout &lt;&lt; " regx = " &lt;&lt; argv[i+1] &lt;&lt; endl; cout &lt;&lt; " fn = " &lt;&lt; argv[i+2] &lt;&lt; endl; i = i+3; continue; } else { cout &lt;&lt; " regx = none" &lt;&lt; endl; cout &lt;&lt; " fn = " &lt;&lt; argv[i] &lt;&lt; endl; i++; continue; } } </code></pre> <p>Note: assume this is written for a unix environment. I don't think I can use anything from the standard library. I only included std::cout for testing purposes.</p> <p>I will be happy to elaborate on any details of the assignment. However, the main question revolves around the syntax of the optstring. I am currently only aware of : meaning required and :: meaning optional is there a way to specify arguments that repeat like a regex wildcard *?</p> <p>EDIT:</p> <p>I'm sure this is sloppy due to the fact that I don't think getopt is designed to handle multiple arguments per option but it does the trick...</p> <pre><code>int main(int argc, char *argv[]){ char c; int iterations = 0; while (*argv) { optind = 1; if (iterations == 0){ opterr = 0; c = getopt(argc, argv, "a"); if(c == 'a'){ //~ APPEND SET } else if(c=='?'){ optind--; } } while ((c = getopt(argc, argv, "f:")) != -1) { if (c == 'f'){ //~ REGEX = optarg if (optind &lt; argc &amp;&amp; strcmp(argv[optind], "-f") != 0) { //~ FILENAME = argv[optind] optind++; } else { errno = 22; perror("Error"); exit(errno); } } else { errno = 22; perror("Error"); exit(errno); } } argc -= optind; argv += optind; iterations++; //~ REMAINING FILES = *argv } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload