agrego practicas
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include<unistd.h>
|
||||
#include<getopt.h>
|
||||
|
||||
|
||||
static struct option option_args[] = {
|
||||
{"help", no_argument, 0, 'h' },
|
||||
{"verbose", no_argument, 0, 'v' },
|
||||
{"output", required_argument, 0, 'o' },
|
||||
{"max", required_argument, 0, 'm' },
|
||||
{0,0,0,0}
|
||||
};
|
||||
|
||||
void print_usage(char* arg){
|
||||
fprintf(stdout, "Uso: %s\n", arg);
|
||||
fprintf(stdout, "[--help]\n");
|
||||
fprintf(stdout, "[--verbose]\n");
|
||||
fprintf(stdout, "[--output <archivo>]\n");
|
||||
fprintf(stdout, "[--max <num>]\n");
|
||||
fprintf(stdout, "[posicionales...]\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
|
||||
printf("argv[0]: %s\n", argv[0]);
|
||||
|
||||
int opt;
|
||||
int opt_index = 0;
|
||||
|
||||
while((opt = getopt_long(argc, argv, "hvo:m:", option_args, &opt_index)) != -1){
|
||||
switch (opt){
|
||||
case 'h':
|
||||
print_usage(argv[0]);
|
||||
exit(0);
|
||||
break;
|
||||
case 'v':
|
||||
fprintf(stdout, "Modo verbose: ON\n");
|
||||
break;
|
||||
case 'o':
|
||||
fprintf(stdout, "Output: %s\n", optarg);
|
||||
break;
|
||||
case 'm':
|
||||
errno = 0;
|
||||
char* errorptr = NULL;
|
||||
long maximo = strtol(optarg, &errorptr, 10);
|
||||
if( (errno == EINVAL)
|
||||
|| (errno == ERANGE)
|
||||
|| (*errorptr != 0)
|
||||
|| (maximo < INT_MIN)
|
||||
|| (maximo > INT_MAX)
|
||||
){
|
||||
fprintf(stderr, "--max debe ser un numero entero valido.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Maximo:%ld\n", maximo);
|
||||
break;
|
||||
case '?':
|
||||
print_usage(argv[0]);
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stdout, "Posicionales:\n");
|
||||
for(int i = optind; i < argc; i++){
|
||||
fprintf(stdout, "%s\n", argv[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user