summaryrefslogtreecommitdiff
path: root/src/cli/opt_usage.c
blob: 478b416316d6e97d8e8c559b1cee6339aa564473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "cli.h"
#include "str.h"

static int print_spec_name(git_str *out, const cli_opt_spec *spec)
{
	if (spec->type == CLI_OPT_TYPE_VALUE && spec->alias &&
	    !(spec->usage & CLI_OPT_USAGE_VALUE_OPTIONAL) &&
	    !(spec->usage & CLI_OPT_USAGE_SHOW_LONG))
		return git_str_printf(out, "-%c <%s>", spec->alias, spec->value_name);
	if (spec->type == CLI_OPT_TYPE_VALUE && spec->alias &&
	    !(spec->usage & CLI_OPT_USAGE_SHOW_LONG))
		return git_str_printf(out, "-%c [<%s>]", spec->alias, spec->value_name);
	if (spec->type == CLI_OPT_TYPE_VALUE &&
	    !(spec->usage & CLI_OPT_USAGE_VALUE_OPTIONAL))
		return git_str_printf(out, "--%s[=<%s>]", spec->name, spec->value_name);
	if (spec->type == CLI_OPT_TYPE_VALUE)
		return git_str_printf(out, "--%s=<%s>", spec->name, spec->value_name);
	if (spec->type == CLI_OPT_TYPE_ARG)
		return git_str_printf(out, "<%s>", spec->value_name);
	if (spec->type == CLI_OPT_TYPE_ARGS)
		return git_str_printf(out, "<%s>...", spec->value_name);
	if (spec->type == CLI_OPT_TYPE_LITERAL)
		return git_str_printf(out, "--");
	if (spec->alias && !(spec->usage & CLI_OPT_USAGE_SHOW_LONG))
		return git_str_printf(out, "-%c", spec->alias);
	if (spec->name)
		return git_str_printf(out, "--%s", spec->name);

	GIT_ASSERT(0);
}

/*
 * This is similar to adopt's function, but modified to understand
 * that we have a command ("git") and a "subcommand" ("checkout").
 * It also understands a terminal's line length and wrap appropriately,
 * using a `git_str` for storage.
 */
int cli_opt_usage_fprint(
	FILE *file,
	const char *command,
	const char *subcommand,
	const cli_opt_spec specs[])
{
	git_str usage = GIT_BUF_INIT, opt = GIT_BUF_INIT;
	const cli_opt_spec *spec;
	size_t i, prefixlen, linelen;
	bool choice = false, next_choice = false, optional = false;
	int error;

	/* TODO: query actual console width. */
	int console_width = 80;

	if ((error = git_str_printf(&usage, "usage: %s", command)) < 0)
		goto done;

	if (subcommand &&
	    (error = git_str_printf(&usage, " %s", subcommand)) < 0)
		goto done;

	linelen = git_str_len(&usage);
	prefixlen = linelen + 1;

	for (spec = specs; spec->type; ++spec) {
		if (!choice)
			optional = !(spec->usage & CLI_OPT_USAGE_REQUIRED);

		next_choice = !!((spec + 1)->usage & CLI_OPT_USAGE_CHOICE);

		if (spec->usage & CLI_OPT_USAGE_HIDDEN)
			continue;

		if (choice)
			git_str_putc(&opt, '|');
		else
			git_str_clear(&opt);

		if (optional && !choice)
			git_str_putc(&opt, '[');
		if (!optional && !choice && next_choice)
			git_str_putc(&opt, '(');

		if ((error = print_spec_name(&opt, spec)) < 0)
			goto done;

		if (!optional && choice && !next_choice)
			git_str_putc(&opt, ')');
		else if (optional && !next_choice)
			git_str_putc(&opt, ']');

		if ((choice = next_choice))
			continue;

		if (git_str_oom(&opt)) {
			error = -1;
			goto done;
		}

		if (linelen > prefixlen &&
		    console_width > 0 &&
		    linelen + git_str_len(&opt) + 1 > (size_t)console_width) {
			git_str_putc(&usage, '\n');

			for (i = 0; i < prefixlen; i++)
				git_str_putc(&usage, ' ');

			linelen = prefixlen;
		} else {
			git_str_putc(&usage, ' ');
			linelen += git_str_len(&opt) + 1;
		}

		git_str_puts(&usage, git_str_cstr(&opt));

		if (git_str_oom(&usage)) {
			error = -1;
			goto done;
		}
	}

	error = fprintf(file, "%s\n", git_str_cstr(&usage));

done:
	error = (error < 0) ? -1 : 0;

	git_str_dispose(&usage);
	git_str_dispose(&opt);
	return error;
}

int cli_opt_usage_error(
	const char *subcommand,
	const cli_opt_spec specs[],
	const cli_opt *invalid_opt)
{
	cli_opt_status_fprint(stderr, PROGRAM_NAME, invalid_opt);
	cli_opt_usage_fprint(stderr, PROGRAM_NAME, subcommand, specs);
	return CLI_EXIT_USAGE;
}

int cli_opt_help_fprint(
	FILE *file,
	const cli_opt_spec specs[])
{
	git_str help = GIT_BUF_INIT;
	const cli_opt_spec *spec;
	int error = 0;

	/* Display required arguments first */
	for (spec = specs; spec->type; ++spec) {
		if (! (spec->usage & CLI_OPT_USAGE_REQUIRED) ||
		    (spec->usage & CLI_OPT_USAGE_HIDDEN))
			continue;

		git_str_printf(&help, "    ");

		if ((error = print_spec_name(&help, spec)) < 0)
			goto done;

		git_str_printf(&help, ": %s\n", spec->help);
	}

	/* Display the remaining arguments */
	for (spec = specs; spec->type; ++spec) {
		if ((spec->usage & CLI_OPT_USAGE_REQUIRED) ||
		    (spec->usage & CLI_OPT_USAGE_HIDDEN))
			continue;

		git_str_printf(&help, "    ");

		if ((error = print_spec_name(&help, spec)) < 0)
			goto done;

		git_str_printf(&help, ": %s\n", spec->help);

	}

	if (git_str_oom(&help) ||
	    p_write(fileno(file), help.ptr, help.size) < 0)
		error = -1;

done:
	error = (error < 0) ? -1 : 0;

	git_str_dispose(&help);
	return error;
}