Article View: es.comp.os.linux.programacion
Article #12808Re: Parsear l�nea de comandos en C
From: Gonzalo Paniagua Javier
Date: Thu, 10 May 2001 20:58
Date: Thu, 10 May 2001 20:58
44 lines
2223 bytes
2223 bytes
crisp <esta@no.es.mi.direccion.verdadera> wrote:
> Estoy haciendo un peque�o programita en GNU/Linux en C, y quiero que
> los par�metros que se le pasen puedan ser del tipo corto y largo:
> Ej: miprograma -i eth0 == miprograma --interface eth0
> miprograma -h == miprograma --help
Espero que esto te irva de ejemplo. Falta el control de errores, pero
espero que te apa�es.
-------------------------
#include <stdio.h>
#include <getopt.h>
int main(int argc, char *argv[]){
static struct option long_options[] =
{
/* descripci�n de los campos:
* 1.- nombre opcion larga
* 2.- tiene args?
* 3.- Si no es NULL, debe apuntar a una zona de memoria
* donde guardar el valor del campo 4.
* 4.- Valor a guardar en 3 si no es NULL o a devolver.
* conviene poner la letra correspondiente de la opci�n corta.
*/
{"interface", 1, NULL, 'i'},
{"help", 1, NULL, 'h'},
{0, 0, NULL, 0}
};
while ((i = getopt_long(argc, argv, "i:d", long_options, NULL)) != -1){
switch(i){
case 'i':
/* optarg contiene el argumento de la opcion */
.........
break;
case 'h':
.........
break;
}
}
}
-----------------------------
Technical Details
Message-ID:
<37ved9.3h.ln@lalo>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!62.112.0.25!newsfeed.online.be!ams.uu.net!newsf01.!newsr01.ono.com.POSTED!not-for-mailReferences:
<shskftg73jas41fjg68rivble3d5mi95g4@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: f86c8,81a5aeecbc4fa4c2
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2001-05-10 13:58:23 PST
Path: arc1-ca.pugleaf.net!newsfeed.google.com!newsfeed.stanford.edu!news.tele.dk!62.112.0.25!newsfeed.online.be!ams.uu.net!newsf01.!newsr01.ono.com.POSTED!not-for-mail
From: Gonzalo Paniagua Javier <gpanjav@jazzfree.com>
Subject: Re: Parsear l�nea de comandos en C
Newsgroups: es.comp.os.linux.programacion
References: <shskftg73jas41fjg68rivble3d5mi95g4@4ax.com>
User-Agent: tin/1.4.4-20000803 ("Vet for the Insane") (UNIX) (Linux/2.4.2-0.1.28 (i586))
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Message-ID: <37ved9.3h.ln@lalo>
Lines: 44
Date: Thu, 10 May 2001 20:58:13 GMT
NNTP-Posting-Host: 62.42.15.125
X-Trace: newsr01.ono.com 989528293 62.42.15.125 (Thu, 10 May 2001 22:58:13 MET DST)
NNTP-Posting-Date: Thu, 10 May 2001 22:58:13 MET DST