[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Нужен ли bash



AC>>> Хорошую вещь придумали - _рассмотрим_, например, getdelim(3)
AC>>> или getline(3). Бред сивой кобылы, как например опции после файлов -
AC>>> на помойку. Именно так и никак иначе.
AC>> опции после файлов - это юзабельность,
AC> Нет! Никакой юзабельности в опциях после файлов нет.
сплошная юзабельность

AC> Почитай таки man 3 getopt
AC> и пойми наконец, КАК ОН РАБОТАЕТ. Я даже сделаю ЗА ТЕБЯ часть работы

AC> If there are no more option characters, getopt() returns -1.
AC> Then optind is the index in argv of the first argv-element that
AC> is not an option.

AC> Теперь домашнее задание. Откуда берется список ФАЙЛОВ на обработку???
из командной строки, (GNU-шный) getopt отродясь спокойно перестраивал
список в ком-строке под то чтобы optind показывал куда надо, это только
"бдящие стандарт" бсд-шники не могут себе представить алгоритма как
отличить опцию от файла, поэтому у них нескладуха в головах, лови тест:

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

char * ptr;

void print_arguments(int argc, char * const argv[])
{
    int opt;
    printf("argument list\n");
    for (opt=0; opt<argc; opt++)
        printf("\tinput argiment: %s\n", argv[opt]);
}

int main(int argc, char * const argv[])
{
    int opt;
    print_arguments(argc, argv);
    while((opt = getopt(argc, argv, "ab:c"))!=-1)
    {
        switch(opt)
        {
            case 'a':
                printf("option 'a' found\n");
                break;
            case 'b':
                printf("option 'b' found, arg=%s\n", optarg);
                break;

            case 'c':
                printf("option 'c' found\n");
                break;

            case '?':
                printf("unknown option found");
                exit(-1);
        }
    }

    printf("optind=%d, argc=%d\n", optind, argc);

    for (opt=optind; opt<argc; opt++)
        printf("argument after options: %s\n", argv[opt]);

    print_arguments(argc, argv);
}

результат работы такой:

nbw:[~]$ ./test -a file1 file2 -c -b optionb   
argument list
    input argiment: ./test
    input argiment: -a
    input argiment: file1
    input argiment: file2
    input argiment: -c
    input argiment: -b
    input argiment: optionb
option 'a' found
option 'c' found
option 'b' found, arg=optionb
optind=5, argc=7
argument after options: file1
argument after options: file2
argument list
    input argiment: ./test
    input argiment: -a
    input argiment: -c
    input argiment: -b
    input argiment: optionb
    input argiment: file1
    input argiment: file2

--

. ''`. Dmitry E. Oboukhov
: :’  : unera@debian.org
`. `~’ GPGKey: 1024D / F8E26537 2006-11-21
  `- 1B23 D4F8 8EC0 D902 0555  E438 AB8C 00CF F8E2 6537

Attachment: signature.asc
Description: Digital signature


Reply to: