Форум: Форум C++Разное
Новые темы: 00
Программирование. Ступени успешной карьеры. Авторы: Кузнецов М.В., Симдянов И.В. PHP Puzzles. Авторы: Кузнецов М.В., Симдянов И.В. PHP на примерах (2 издание). Авторы: Кузнецов М.В., Симдянов И.В. MySQL 5. В подлиннике. Авторы: Кузнецов М.В., Симдянов И.В. PHP 5. На примерах. Авторы: Кузнецов М.В., Симдянов И.В., Голышев С.В.
ВСЕ НАШИ КНИГИ
Консультационный центр SoftTime

Форум C++

Выбрать другой форум

 

Здравствуйте, Посетитель!

вид форума:
Линейный форум Структурный форум

тема: опции командной строки - howto?
 
 автор: DPbIH   (15.10.2007 в 16:38)   письмо автору
 
 

Всем привет!
Хочу длля своего приложения создать опции командной строки, чтобы не привязываться к порядку следования параметров. Подскажите как это реализовать?
Спасибо!

  Ответить  
 
 автор: cheops   (15.10.2007 в 16:43)   письмо автору
 
   для: DPbIH   (15.10.2007 в 16:38)
 

Создайте массив обработчиков (а по сути функций, обрабатывающих тот или иной параметр) и прогоняйте каждый встреченный вами параметр в цикле через этот массив, отбирая обработчик по принципу подходи-не подходит.

  Ответить  
 
 автор: like-nix   (16.10.2007 в 20:12)   письмо автору
 
   для: DPbIH   (15.10.2007 в 16:38)
 

Вообще в linux есть спец функция getopt_long
/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
* Copyright (C) 2001 by New Riders Publishing *
* See COPYRIGHT for license information. *
***********************************************************************/

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

/* The name of this program. */
const char* program_name;

/* Prints usage information for this program to STREAM (typically
stdout or stderr), and exit the program with EXIT_CODE. Does not
return. */

void print_usage (FILE* stream, int exit_code)
{
fprintf (stream, "Usage: %s options [ inputfile ... ]\n", program_name);
fprintf (stream,
" -h --help Display this usage information.\n"
" -o --output filename Write output to file.\n"
" -v --verbose Print verbose messages.\n");
exit (exit_code);
}

/* Main program entry point. ARGC conains number of argument list
elements; ARGV is an array of pointers to them. */

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

/* A string listing valid short options letters. */
const char* const short_options = "ho:v";
/* An array describing valid long options. */
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "output", 1, NULL, 'o' },
{ "verbose", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 } /* Required at end of array. */
};

/* The name of the file to receive program output, or NULL for
standard output. */
const char* output_filename = NULL;
/* Whether to display verbose messages. */
int verbose = 0;

/* Remember the name of the program, to incorporate in messages.
The name is stored in argv[0]. */
program_name = argv[0];

do {
next_option = getopt_long (argc, argv, short_options,
long_options, NULL);
switch (next_option)
{
case 'h': /* -h or --help */
/* User has requested usage information. Print it to standard
output, and exit with exit code zero (normal termination). */
print_usage (stdout, 0);

case 'o': /* -o or --output */
/* This option takes an argument, the name of the output file. */
output_filename = optarg;
break;

case 'v': /* -v or --verbose */
verbose = 1;
break;

case '?': /* The user specified an invalid option. */
/* Print usage information to standard error, and exit with exit
code one (indicating abonormal termination). */
print_usage (stderr, 1);

case -1: /* Done with options. */
break;

default: /* Something else: unexpected. */
abort ();
}
}
while (next_option != -1);

/* Done with options. OPTIND points to first non-option argument.
For demonstration purposes, print them if the verbose option was
specified. */
if (verbose) {
int i;
for (i = optind; i < argc; ++i)
printf ("Argument: %s\n", argv[i]);
}

/* The main program goes here. */

return 0;
}

  Ответить  
Rambler's Top100
вверх

Rambler's Top100 Яндекс.Метрика Яндекс цитирования