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

Pages: 1-

Option parsing.

Name: Anonymous 2008-05-18 21:18

I just wrote two versions of a simple option parsing function with that supports, specifically, --long options with parameters.

Which one do you prefer? (I am not asking you about style, and don't tell me to use existing [sub]and bloated[sub] libraries.)

The first one stores long options in a "previous" variable:


#include <stdio.h>

int main(int argc, char *argv[]) {
    int i;
    char *previous = NULL;

    for (i = 1; i < argc; i++) {
        if (!previous && !strncmp(argv[i], "--", 2)) {
            /*if option starts with --*/
            previous = argv[i];
            continue;  /*skip assignment of previous*/
        } else if (previous && !strcmp(previous, "--long")) {
            /*if previous option was --long*/
            printf("ZOMG %s is LOOONG\n", argv[i]);
        } else if (previous) {
            goto lostthegame;
        } else {
            printf("%s\n", argv[i]);
        }
        previous = NULL; /*NULL NULL NULL NULL NULL*/
    }

    if (!previous) return 0;

    lostthegame:
    printf("Unrecognized option, sorry, you lost the game\n");
    return 1;
}


The second one increments i to access the next array element directly:


#include <stdio.h>

int main(int argc, char *argv[]) {
    int i;

    for (i = 1; i < argc; i++) {
        if (!strncmp(argv[i], "--", 2)) {
            /*if option starts with --*/
            if (++i == argc)
                /*and is not the last arg*/
                goto lostthegame;
           
            if (!strcmp(argv[i - 1], "--long"))
                printf("ZOMG %s is LOOONG\n", argv[i]);
            else
                goto lostthegame;
           
        } else {
            printf("%s\n", argv[i]);
        }
    }
    return 0;

    /*this makes less sense in this version*/
    lostthegame:
    printf("Unrecognized option, sorry, you lost the game\n");
    return 1;
}


Both are just simple ideas I got. I'm just asking you, EXPERT PROGRAMMERS how ``hackish'' do you think it is

Name: Anonymous 2008-05-18 21:45

null is not necessarily false

Name: Anonymous 2008-05-18 23:08

>--long options
GNUfag

Name: Anonymous 2008-05-18 23:29

>>1
just use getopt, faggot.

Name: Anonymous 2008-05-18 23:45

>>2
Null isn't necessarily true.

Name: Anonymous 2008-05-18 23:48

>>5

Null is necessarily nil.

Name: Anonymous 2008-05-18 23:50

>>6
Too bad Ruby is slow as fuck.

Name: Anonymous 2008-05-19 0:00

>>7
Who said anything about Ruby?

Name: Anonymous 2008-05-19 0:08

Nil is necessarily listp.

Name: Anonymous 2008-05-19 0:54

FAILS hard for goto

Name: Anonymous 2008-05-19 0:58

if (foo)
{
    goto success;
}
else
{
    goto failure;
}
success:
    return 1;
failure:
    return 0;

Name: Anonymous 2008-05-19 1:14

Too bad slow is Ruby as fuck.

Name: Anonymous 2008-05-19 7:09

foo ? return 1 : return 0;

Name: Anonymous 2008-05-19 7:14

>>13
return !!foo;

Name: Anonymous 2008-05-19 18:38

>>14
return foo;

Name: Anonymous 2008-05-19 19:21

>>15
return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!foo;

Name: OP 2008-05-19 20:07

>>3
lolwut?

>>2,5
Right

>>10
Intentional usage just to troll faggots like you. That is the only encouraged use of goto.

>>11
lol'd

Name: Anonymous 2008-05-19 20:08

"FAILS hard for goto" considered harmful.

Name: Anonymous 2008-05-19 20:31

Being against goto is just an old, crappy meme

Name: Anonymous 2008-05-19 20:46

ls <parameters><long>1</long><humanreadable>1</humanreadable></parameters>

Name: Anonymous 2008-05-19 20:50

The Knuth supports the goto.

Name: Anonymous 2008-05-19 21:36

>>20
ENTERPRISE

Name: Anonymous 2008-05-20 2:40

>>19
It's all about come from, baby!

Name: Anonymous 2008-05-20 5:02

COBOL was the peak of GOTO technology. It's all been downhill since.

Name: Anonymous 2008-05-20 10:18

I USE GETOPT IS SO COOL

Name: Anonymous 2008-05-20 14:50

[m]#INCLUDE <STDIOH>
INT!11!1 WTF MANE(INT ARGC CHAR *ARGV[]) {
 INT I
 CHAR *PRAVIOS = NUL
 FOR (I = 1 I < ARGC I+) {
 IF (PR3VIOS!!!!!11!1 OMG WTF & STRNCMP(ARGV[I]!1111 LOL - 2) {
 /*IF OPTION STARTS WIT -*/
 PR3VIOS = ARGV[I]
 CONTINUE /*SKIP ASIGNMENT OF PREVIOS*/
 } ELS3 IF (PREVIOS & STRCMP(PREVIOS!!111 OMG -LONG) {
 /*IF PR3VIOS OPTION WAS -LONG*/
 PRINTF(ZOMG %S SI LOONG\N ARGV[I])
 } 3LSA IF (PRAVIOS) {
 GO2 LOSTHEGMA
 } 3LSA {
 PRINTF(%S\N ARGV[I])
 }
 PREVIOS = NUL /*NUL NUL NUL NUL NUL*/
 }
 IF (PRAVIOS)!!!1!1 WTF RATURN 0
 LOSTHEGMA
 PRINTF(UNR3COGNIEZD OPTION SORY U LOST DA GME\N)
 RETURN 1
}[/m]

Name: Anonymous 2008-05-20 14:50

[m]
#INCLUDE <STDIOH>
INT!11!1 WTF MANE(INT ARGC CHAR *ARGV[]) {
 INT I
 CHAR *PRAVIOS = NUL
 FOR (I = 1 I < ARGC I+) {
 IF (PR3VIOS!!!!!11!1 OMG WTF & STRNCMP(ARGV[I]!1111 LOL - 2) {
 /*IF OPTION STARTS WIT -*/
 PR3VIOS = ARGV[I]
 CONTINUE /*SKIP ASIGNMENT OF PREVIOS*/
 } ELS3 IF (PREVIOS & STRCMP(PREVIOS!!111 OMG -LONG) {
 /*IF PR3VIOS OPTION WAS -LONG*/
 PRINTF(ZOMG %S SI LOONG\N ARGV[I])
 } 3LSA IF (PRAVIOS) {
 GO2 LOSTHEGMA
 } 3LSA {
 PRINTF(%S\N ARGV[I])
 }
 PREVIOS = NUL /*NUL NUL NUL NUL NUL*/
 }
 IF (PRAVIOS)!!!1!1 WTF RATURN 0
 LOSTHEGMA
 PRINTF(UNR3COGNIEZD OPTION SORY U LOST DA GME\N)
 RETURN 1
}
[/m]

Name: Anonymous 2008-05-22 7:29

Both suck ass, option parsing is best implemented as a state machine.

Name: Anonymous 2008-05-22 19:19

>>26,27
wat

>>28
Monads?

Name: Anonymous 2008-05-25 20:51

GOTO is a martyr against ENTERPRISEness.

I support it.

Name: Anonymous 2008-05-25 23:06

>>30
Interesting point of view. Can you explain your idea better?

Name: Anonymous 2008-05-25 23:39

>>31
The Knuth supports GOTO.

Name: Anonymous 2008-05-25 23:41

>>32
I think I've heard this before

Name: Anonymous 2008-05-25 23:42

Name: Trollbot9000 2009-07-01 8:28

The This may surprise.

Name: Anonymous 2011-02-03 5:37

Name: Sgt.Kabukiman꣧ 2012-05-23 5:56

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

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