added ffplay utility
[libav.git] / cmdutils.c
CommitLineData
01310af2
FB
1/*
2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22
23#include "common.h"
24#include "avformat.h"
25
26#include "cmdutils.h"
27
28void show_help_options(const OptionDef *options)
29{
30 const OptionDef *po;
31 int i, expert, first;
32
33 printf("Main options are:\n");
34 for(i=0;i<2;i++) {
35 first = 1;
36 for(po = options; po->name != NULL; po++) {
37 char buf[64];
38 expert = (po->flags & OPT_EXPERT) != 0;
39 if (expert == i) {
40 if (expert && first) {
41 printf("\nAdvanced options are:\n");
42 first = 0;
43 }
44 strcpy(buf, po->name);
45 if (po->flags & HAS_ARG) {
46 strcat(buf, " ");
47 strcat(buf, po->argname);
48 }
49 printf("-%-17s %s\n", buf, po->help);
50 }
51 }
52 }
53}
54
55void parse_options(int argc, char **argv, const OptionDef *options)
56{
57 const char *opt, *arg;
58 int optindex;
59 const OptionDef *po;
60
61 /* parse options */
62 optindex = 1;
63 while (optindex < argc) {
64 opt = argv[optindex++];
65
66 if (opt[0] == '-' && opt[1] != '\0') {
67 po = options;
68 while (po->name != NULL) {
69 if (!strcmp(opt + 1, po->name))
70 break;
71 po++;
72 }
73 if (!po->name) {
74 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
75 exit(1);
76 }
77 arg = NULL;
78 if (po->flags & HAS_ARG) {
79 arg = argv[optindex++];
80 if (!arg) {
81 fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
82 exit(1);
83 }
84 }
85 if (po->flags & OPT_STRING) {
86 char *str;
87 str = strdup(arg);
88 *po->u.str_arg = str;
89 } else if (po->flags & OPT_BOOL) {
90 *po->u.int_arg = 1;
91 } else {
92 po->u.func_arg(arg);
93 }
94 } else {
95 parse_arg_file(opt);
96 }
97 }
98}
99
100void print_error(const char *filename, int err)
101{
102 switch(err) {
103 case AVERROR_NUMEXPECTED:
104 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
105 "Use '%%d' to specify the image number:\n"
106 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
107 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
108 filename);
109 break;
110 case AVERROR_INVALIDDATA:
111 fprintf(stderr, "%s: Error while parsing header\n", filename);
112 break;
113 case AVERROR_NOFMT:
114 fprintf(stderr, "%s: Unknown format\n", filename);
115 break;
116 default:
117 fprintf(stderr, "%s: Error while opening file\n", filename);
118 break;
119 }
120}