Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Программирование под Unix/Linux > парсинг аргументов командной строки


Автор: _hunter 13.2.2008, 14:39
Добрый день.

Подскажите как парсится командная строка в никсах.
Т.е. если команда такая: "some_progrma qwe      rty" (т.е. между параметрами несколько пробелов (возможно табов)) -- что будет в argv? -- "qwe" и "rty" или какие-то вариации на тему пробелов?

С уважением...

Автор: JackYF 13.2.2008, 16:19
Цитата(_hunter @  13.2.2008,  13:39 Найти цитируемый пост)
"qwe" и "rty"

ЕМНИП, да. А вообще можно было и самому проверить вроде бы smile

Автор: _hunter 13.2.2008, 16:23
самому проверить сложно -- это Linux ставить нужно...

Автор: JackYF 13.2.2008, 18:00
Цитата(_hunter @  13.2.2008,  15:23 Найти цитируемый пост)
это Linux ставить нужно... 

а. ну сейчас проверю...

Автор: MAKCim 13.2.2008, 18:02
Цитата(_hunter @  13.2.2008,  16:23 Найти цитируемый пост)
самому проверить сложно -- это Linux ставить нужно... 

разделители не учитываются

Автор: mr_oz 13.2.2008, 18:02
точно не знаю но вот есть цитата... 

Цитата


:  parser, generic parser, UNIX, ARGC, ARGV
----------------:
--
-- Abstract     :  
--
--|   PARSER is a generic parser that functions in a manner similar to
--| the ARGC/ARGV parser of UNIX.  It contains one procedure, PARSE,
--| which accepts a string as input and returns ARGC, a count of the number
--| of tokens in the string, and ARGV, a vector of strings, each string
--| containing a token.
--|
--|   PARSER is instantiated with two strings (DEL for DELIMITER and
--| DEL_TOKEN for DELIMITER_TOKEN).  The DEL string is composed of characters
--| which delimit each token (and are not a part of the token).  All
--| characters less than space are automatically delimiters, and the DEL
--| string should contain at least one character (such as a space).
--| DEL_TOKEN is a string composed of characters which delimit tokens and
--| which are tokens themselves.  If "=" is a DEL_TOKEN, for example, then
--| "CAT= DOG" is composed of three tokens, "CAT", "=", and "DOG", where if
--| "=" is a DEL, then "CAT= DOG" is composed of two tokens, "CAT" and "DOG".
--| This assumes that the space character is a DEL.
--|
--|   PARSER may also be instantiated with ARGC_LIMIT, which indicates the
--| maximum number of tokens allowed.  If this limit is exceeded, then the
--| last ARGV token contains the remainder of the string.  The default value
--| of ARGC_LIMIT is 20.
--|
--|   ARG_STRING_LENGTH is the last instantiation option for PARSER.  It
--| indicates the maximum length of an ARGV string, and it defaults to 80.
--|


точно не знаю, тебя это интересует.

Автор: JackYF 13.2.2008, 18:06
Цитата

jackyf@work:~/temp/tests$ cat test.cpp
#include <iostream>
int main(int argc, char* argv[])
{
    for (int i = 0; i < argc; ++i)
    {
  std::cout << '|' << argv[i] << '|' << std::endl;
    }
}

jackyf@work:~/temp/tests$ ./test.e    aaa        bbb
|./test.e|
|aaa|
|bbb|

Автор: nickless 13.2.2008, 19:37
http://www.gnu.org/software/bash/manual/bashref.html#Word-Splitting

Автор: bsa 14.2.2008, 13:48
Если я не ошибаюсь, аргументы парсются так же, как и для консольных приложений Windows. Т.е. если взять параметр в кавычки (одинарные или двойные) или экранировать спецсимволы обратным слешем, то будут переданы в том числе и пробелы:
Код
$ ./mytestprog abc def ghi jkl
abc
def
ghi
jkl
$ ./mytestprog "abc def" 'ghi jkl'
abc def
ghi jkl
$ ./mytestprog abc def\ ghi jkl
abc
def ghi
jkl

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)