Passando parâmetros com getopt
Publicado por Andre Luiz da Silva 02/05/2004
[ Hits: 11.839 ]
Homepage: http://localhost
Esse é um simples código demonstrando como manipular parâmetros passados para um programa usando a função "getopt".
#include <stdio.h> #include <unistd.h> #include <errno.h> void help() { printf ("Olah.... eu sou o a funcao help!"); } void verbose () { printf ("Olah... eu sou a verbose!! \n"); } int print (char *file, char *string) { FILE *fd; if ((fd = fopen (file, "w")) == NULL) { printf ("fopen(): %s \n", strerror(errno)); exit (1); } fprintf (fd, "%s", string); fclose (fd); return 0; } int opterr = 0; int main(int argc, char **argv) { char *file = NULL, *phase = NULL; if (argc == 1) { printf ("Nao tem argumento! Falowz... \n"); exit (0); } while (1) { int opt; opt = getopt (argc, argv, "hvp:f:"); switch (opt) { case 'h': help(); break; case 'v': verbose(); break; case 'p': phase = optarg; break; case 'f': file = optarg; break; } if (opt == -1) break; if (opt == '?') { printf ("%c: This option don't exists!\n", optopt); break; } } if (phase != NULL && file != NULL) { print (file, phase); exit(0); } return 0; }
Sintetizador sonoro que toca redez vouz 4
Manipulando argumentos com getopt_long
Atualizando o Passado: Linux no Lenovo G460 em 2025
aaPanel - Um Painel de Hospedagem Gratuito e Poderoso
O macete do Warsaw no Linux Mint e cia
Visualizar arquivos em formato markdown (ex.: README.md) pelo terminal
Dando - teoricamente - um gás no Gnome-Shell do Arch Linux
Como instalar o Google Cloud CLI no Ubuntu/Debian
Mantenha seu Sistema Leve e Rápido com a Limpeza do APT!
Procurando vídeos de YouTube pelo terminal e assistindo via mpv (2025)
debian com wayland, configuracao de teclado. Mudou Tudo! [RESOLVIDO] (1)
Iinstalar o Scanner Kodak i940 no Linux Mint 19/20? (4)
Pastas da raiz foram para a área de trabalho [RESOLVIDO] (11)