Using getopt_long with more than just a one letter character code.


 
Thread Tools Search this Thread
Top Forums Programming Using getopt_long with more than just a one letter character code.
# 1  
Old 03-26-2012
Using getopt_long with more than just a one letter character code.

When using getopt_long in g++ I get an error

Code:
./source/GetVel.cc:122:40: warning: multi-character character constant
./source/GetVel.cc:162:14: warning: multi-character character constant

Part of the code is shown below. As I might have a lot of options, I am thinking
of using the option name rather than just one letter character code. For example, using 'cmod'.

Code:
    static struct option long_options[] = {
        {"cmod", required_argument, 0, 'cmod'},
        {"append", 1, 0, 0},
        {"delete", 1, 0, 0},
        {"verbose", 1, 0, 0},
        {"help", no_argument, 0, 'h'},
        {"file", 1, 0, 0},
        {NULL, 0, NULL, 0}                       // Required at end of array.
    };

    while ((c = getopt_long(argc, argv, "abc:d:012",
                 long_options, &option_index)) != -1) {
        int this_option_optind = optind ? optind : 1;
        switch (c) {
        case 0:
            printf ("option %s", long_options[option_index].name);
            if (optarg)
                printf (" with arg %s", optarg);
            printf ("\n");
            break;
        case 'cmod':
            printf ("option cmod with value '%s'\n", optarg);
            opt_cmod = optarg;
            break;

---------- Post updated at 09:04 AM ---------- Previous update was at 07:15 AM ----------

Does anybody know what is the meaning of "abc:d:012" ?

---------- Post updated at 09:59 AM ---------- Previous update was at 09:04 AM ----------

I have figured out that The : after the letter means that the option requires a value, but have not been able to decipher the meaning of 012.
# 2  
Old 03-26-2012
There are many numbering systems used in computers...hex octal decimal and binary...so now you have to just narrow it down to the right one...
# 3  
Old 03-26-2012
Quote:
Originally Posted by shamrock
There are many numbering systems used in computers...hex octal decimal and binary...so now you have to just narrow it down to the right one...
Cannot understand exactly what you mean. An example might help.
# 4  
Old 03-26-2012
Quote:
Originally Posted by kristinu
Cannot understand exactly what you mean. An example might help.
The number 012 is octal code for the newline character...
# 5  
Old 03-26-2012
I see.

---------- Post updated at 11:32 AM ---------- Previous update was at 11:30 AM ----------

I have put my code as below. As I might have a lot of options I am using an integer for val rather than a character except for short options and help.

Code:
    // A string listing valid short options letters.
    const char* const short_options = "hv:";

    // An array describing valid long options.
    static struct option long_options[] = {
        {"cmod", required_argument, 0, 10},
        {"vdx",  required_argument, 0, 11},
        {"kx",   required_argument, 0, 12},
        {"vdz",  required_argument, 0, 13},
        {"kz",   required_argument, 0, 14},
        {"idx",  required_argument, 0, 15},
        {"ki",   required_argument, 0, 16},
        {"vel",  required_argument, 0, 17},
        {"velp", required_argument, 0, 18},
        {"vels", required_argument, 0, 19},
        {"intf", required_argument, 0, 20},
        {"lay",  required_argument, 0, 21},
        {"verbose", optional_argument, 0, 22},
        {"help", no_argument, 0, 'h'},
        {NULL, 0, NULL, 0}                       // Required at end of array.
    };

    int option_index = 0;
    while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
        int this_option_optind = optind ? optind : 1;
        switch (c) {
        case 0:
            printf ("option h number\n");
            printf ("option %s", long_options[option_index].name);
            if (optarg)
                printf (" with arg %s", optarg);
            printf ("\n");
            break;
        case 10:
            printf ("cmod, option 10\n");
            printf ("option cmod with value '%s'\n", optarg);
            break;
        case 11:
            printf ("vdx, option 11\n");
            printf ("option vdx with value '%s'\n", optarg);
            break;
        case 12:
            printf ("kx, option 12\n");
            printf ("option kx with value '%s'\n", optarg);
            break;
        case 13:
            printf ("vdz, option 13\n");
            printf ("option vdz with value '%s'\n", optarg);
            break;
        case 14:
            printf ("kz, option 14\n");
            printf ("option kz with value '%s'\n", optarg);
            break;
        case 15:
            printf ("idx, option 15\n");
            printf ("option idx with value '%s'\n", optarg);
            break;
        case 16:
            printf ("ki, option 16\n");
            printf ("option ki with value '%s'\n", optarg);
            break;
        case 17:
            printf ("vel, option 17\n");
            printf ("option vel with value '%s'\n", optarg);
            break;
        case 18:
            printf ("velp, option 18\n");
            printf ("option velp with value '%s'\n", optarg);
            break;
        case 19:
            printf ("vels, option 19\n");
            printf ("option vels with value '%s'\n", optarg);
            break;
        case 20:
            printf ("intf, option 20\n");
            printf ("option intf with value '%s'\n", optarg);
            break;
        case 21:
            printf ("lay, option 21\n");
            printf ("option lay with value '%s'\n", optarg);
            break;
        case 'a':
            printf ("option a\n");
            aopt = 1;
            break;
        case 'b':
            printf ("option b\n");
            bopt = 1;
            break;
        case 'c':
            printf ("option cmod with value '%s'\n", optarg);
            opt_cmod = optarg;
            break;
        case 'h':
            print_help();
            break;
        case '?':
            break;
        default:
            printf ("?? getopt returned character code 0%o ??\n", c);
        }
    }

    // Process the remaining non-option arguments.
    if (optind < argc) {
        printf ("non-option ARGV-elements: ");
        while (optind < argc)
            printf ("%s ", argv[optind++]);
        printf ("\n");
    }
    exit (0);

# 6  
Old 03-26-2012
So what is the problem...as the code looks good to me...looking cursorily at it.
# 7  
Old 03-26-2012
removed the 012, not sure what difference does it make.

Anyway, program is working. Concerning command line options, do people usually allow option names to be any combination of lower and upper case letters?

Last edited by kristinu; 03-26-2012 at 08:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

3. Programming

Problem of first letter

i have function to send arabic notification email to employee , but when i open the email in outlook i see that the first letter of the caption (employee name) is trimed , but when i open the same email from other workstation i see the caption is complete (working fine) (1 Reply)
Discussion started by: AimyThomas
1 Replies

4. Shell Programming and Scripting

sort -t option causing code to fail need ASCII character

Hello, When I run this UNIX code without the -t option it gives me the desired results. The code keeps the record with the greatest datetime based on the key columns. I sort it first then sort it again with the -u option, that's it. I need to have a variable to specify an ASCII character... (2 Replies)
Discussion started by: script_op2a
2 Replies

5. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

6. UNIX for Dummies Questions & Answers

If first letter of name is a-m do this if n-x do something else

so if I run echo $USER and the name is smithr i want to run command B to run because s is after m in the alphabet, but if the user is aldap I want command A to run because their first initial in the user name is in the first half of alphabet. how please? (2 Replies)
Discussion started by: glev2005
2 Replies

7. Shell Programming and Scripting

what is dead.letter ??

Hi all can you please help me what is dead.letter file ? when it is created ? for the first time i have seen this file getting created in my current directory? I am using SunOs. Any IDEA ?? (2 Replies)
Discussion started by: jambesh
2 Replies

8. UNIX for Dummies Questions & Answers

SORT by letter

Hello again, Since I am UNIX new user I have this question: I have this file: 1 A A 1 A A 1 A A 1 B B 1 B B 1 X X 1 X X 1 C C Could anyone of you help me to sort this file like this: (I need all A and X together) 1 A A 1 A A (1 Reply)
Discussion started by: murbina
1 Replies

9. UNIX for Dummies Questions & Answers

dead.letter

HI, I am pretty new to Unix...but here is 1 serious problem...atleast for me..:-) now..the dead.letter file in /var/tmp has been growin continuously..n i dont know why..I ve even killed the sendmail process..but the dead.letter file keeps on increasin..Can anyone tell me where do I start... (6 Replies)
Discussion started by: unisam
6 Replies
Login or Register to Ask a Question