VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp@ 94210

Last change on this file since 94210 was 94210, checked in by vboxsync, 3 years ago

doc/manual,FE/VBoxManage: Convert metrics command to refentry documentation, ​bugref:9186

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.7 KB
Line 
1/* $Id: VBoxManageHelp.cpp 94210 2022-03-13 20:25:00Z vboxsync $ */
2/** @file
3 * VBoxManage - help and other message output.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/version.h>
23
24#include <iprt/asm.h>
25#include <iprt/buildconfig.h>
26#include <iprt/ctype.h>
27#include <iprt/assert.h>
28#include <iprt/env.h>
29#include <iprt/err.h>
30#include <iprt/getopt.h>
31#include <iprt/stream.h>
32#include <iprt/message.h>
33#include <iprt/uni.h>
34
35#include "VBoxManage.h"
36
37
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41/** If the usage is the given number of length long or longer, the error is
42 * repeated so the user can actually see it. */
43#define ERROR_REPEAT_AFTER_USAGE_LENGTH 16
44
45
46/*********************************************************************************************************************************
47* Global Variables *
48*********************************************************************************************************************************/
49DECLARE_TRANSLATION_CONTEXT(Help);
50
51#ifndef VBOX_ONLY_DOCS
52static enum HELP_CMD_VBOXMANAGE g_enmCurCommand = HELP_CMD_VBOXMANAGE_INVALID;
53/** The scope mask for the current subcommand. */
54static uint64_t g_fCurSubcommandScope = RTMSGREFENTRYSTR_SCOPE_GLOBAL;
55
56/**
57 * Sets the current command.
58 *
59 * This affects future calls to error and help functions.
60 *
61 * @param enmCommand The command.
62 */
63void setCurrentCommand(enum HELP_CMD_VBOXMANAGE enmCommand)
64{
65 Assert(g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID);
66 g_enmCurCommand = enmCommand;
67 g_fCurSubcommandScope = RTMSGREFENTRYSTR_SCOPE_GLOBAL;
68}
69
70
71/**
72 * Sets the current subcommand.
73 *
74 * This affects future calls to error and help functions.
75 *
76 * @param fSubcommandScope The subcommand scope.
77 */
78void setCurrentSubcommand(uint64_t fSubcommandScope)
79{
80 g_fCurSubcommandScope = fSubcommandScope;
81}
82
83
84/**
85 * Takes first char and make it uppercase.
86 *
87 * @returns pointer to string starting from next char.
88 * @param pszSrc Source string.
89 * @param pszDst Pointer to buffer to place first char uppercase.
90 */
91static const char *captialize(const char *pszSrc, char *pszDst)
92{
93 *RTStrPutCp(pszDst, RTUniCpToUpper(RTStrGetCp(pszSrc))) = '\0';
94 return RTStrNextCp(pszSrc);
95}
96
97
98/**
99 * Prints brief help for a command or subcommand.
100 *
101 * @returns Number of lines written.
102 * @param enmCommand The command.
103 * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
104 * for all.
105 * @param pStrm The output stream.
106 */
107static uint32_t printBriefCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
108{
109 /*
110 * Try to find translated, falling back untranslated.
111 */
112 uint32_t cLinesWritten = 0;
113 uint32_t cPendingBlankLines = 0;
114 uint32_t cFound = 0;
115 PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
116 {
117 ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
118#ifdef VBOX_WITH_VBOXMANAGE_NLS
119 &g_aHelpLangEntries[0]
120#endif
121 };
122 for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
123 {
124 /* skip if english is used */
125 if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
126 break;
127 uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
128 for (uint32_t i = 0; i < cHelpEntries; i++)
129 {
130 PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
131 if (pHelp->idInternal == (int64_t)enmCommand)
132 {
133 cFound++;
134 if (cFound == 1)
135 {
136 if (fSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL)
137 {
138 char szFirstChar[8];
139 RTStrmPrintf(pStrm, Help::tr("Usage - %s%s:\n"), szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
140 }
141 else
142 RTStrmPrintf(pStrm, Help::tr("Usage:\n"));
143 }
144 RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Synopsis, fSubcommandScope, &cPendingBlankLines, &cLinesWritten);
145 if (!cPendingBlankLines)
146 cPendingBlankLines = 1;
147 }
148 }
149 }
150 Assert(cFound > 0);
151 return cLinesWritten;
152}
153
154
155/**
156 * Prints the brief usage information for the current (sub)command.
157 *
158 * @param pStrm The output stream.
159 */
160void printUsage(PRTSTREAM pStrm)
161{
162 printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, pStrm);
163}
164
165
166/**
167 * Prints full help for a command or subcommand.
168 *
169 * @param enmCommand The command.
170 * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
171 * for all.
172 * @param pStrm The output stream.
173 */
174static void printFullCommandOrSubcommandHelp(enum HELP_CMD_VBOXMANAGE enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
175{
176 /* Try to find translated, then untranslated */
177 uint32_t cPendingBlankLines = 0;
178 uint32_t cFound = 0;
179 PCHELP_LANG_ENTRY_T const apHelpLangEntries[] =
180 {
181 ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T),
182#ifdef VBOX_WITH_VBOXMANAGE_NLS
183 &g_aHelpLangEntries[0]
184#endif
185 };
186 for (uint32_t k = 0; k < RT_ELEMENTS(apHelpLangEntries) && cFound == 0; k++)
187 {
188 /* skip if english is used */
189 if (k > 0 && apHelpLangEntries[k] == apHelpLangEntries[0])
190 break;
191 uint32_t const cHelpEntries = *apHelpLangEntries[k]->pcHelpEntries;
192 for (uint32_t i = 0; i < cHelpEntries; i++)
193 {
194 PCRTMSGREFENTRY pHelp = apHelpLangEntries[k]->papHelpEntries[i];
195
196 if ( pHelp->idInternal == (int64_t)enmCommand
197 || enmCommand == HELP_CMD_VBOXMANAGE_INVALID)
198 {
199 cFound++;
200 RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Help, fSubcommandScope, &cPendingBlankLines, NULL /*pcLinesWritten*/);
201 if (cPendingBlankLines < 2)
202 cPendingBlankLines = 2;
203 }
204 }
205 }
206 Assert(cFound > 0);
207}
208
209
210/**
211 * Prints the full help for the current (sub)command.
212 *
213 * @param pStrm The output stream.
214 */
215void printHelp(PRTSTREAM pStrm)
216{
217 printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, pStrm);
218}
219
220
221/**
222 * Display no subcommand error message and current command usage.
223 *
224 * @returns RTEXITCODE_SYNTAX.
225 */
226RTEXITCODE errorNoSubcommand(void)
227{
228 Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
229 Assert(g_fCurSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL);
230
231 return errorSyntax(Help::tr("No subcommand specified"));
232}
233
234
235/**
236 * Display unknown subcommand error message and current command usage.
237 *
238 * May show full command help instead if the subcommand is a common help option.
239 *
240 * @returns RTEXITCODE_SYNTAX, or RTEXITCODE_SUCCESS if common help option.
241 * @param pszSubcommand The name of the alleged subcommand.
242 */
243RTEXITCODE errorUnknownSubcommand(const char *pszSubcommand)
244{
245 Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
246 Assert(g_fCurSubcommandScope == RTMSGREFENTRYSTR_SCOPE_GLOBAL);
247
248 /* check if help was requested. */
249 if ( strcmp(pszSubcommand, "--help") == 0
250 || strcmp(pszSubcommand, "-h") == 0
251 || strcmp(pszSubcommand, "-?") == 0)
252 {
253 printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
254 return RTEXITCODE_SUCCESS;
255 }
256
257 return errorSyntax(Help::tr("Unknown subcommand: %s"), pszSubcommand);
258}
259
260
261/**
262 * Display too many parameters error message and current command usage.
263 *
264 * May show full command help instead if the subcommand is a common help option.
265 *
266 * @returns RTEXITCODE_SYNTAX, or RTEXITCODE_SUCCESS if common help option.
267 * @param papszArgs The first unwanted parameter. Terminated by
268 * NULL entry.
269 */
270RTEXITCODE errorTooManyParameters(char **papszArgs)
271{
272 Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
273 Assert(g_fCurSubcommandScope != RTMSGREFENTRYSTR_SCOPE_GLOBAL);
274
275 /* check if help was requested. */
276 if (papszArgs)
277 {
278 for (uint32_t i = 0; papszArgs[i]; i++)
279 if ( strcmp(papszArgs[i], "--help") == 0
280 || strcmp(papszArgs[i], "-h") == 0
281 || strcmp(papszArgs[i], "-?") == 0)
282 {
283 printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
284 return RTEXITCODE_SUCCESS;
285 }
286 else if (!strcmp(papszArgs[i], "--"))
287 break;
288 }
289
290 return errorSyntax(Help::tr("Too many parameters"));
291}
292
293
294/**
295 * Display current (sub)command usage and the custom error message.
296 *
297 * @returns RTEXITCODE_SYNTAX.
298 * @param pszFormat Custom error message format string.
299 * @param va Format arguments.
300 */
301RTEXITCODE errorSyntaxV(const char *pszFormat, va_list va)
302{
303 Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
304
305 showLogo(g_pStdErr);
306
307 va_list vaCopy;
308 va_copy(vaCopy, va);
309 RTMsgErrorV(pszFormat, vaCopy);
310 va_end(vaCopy);
311
312 RTStrmPutCh(g_pStdErr, '\n');
313 if ( printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdErr)
314 >= ERROR_REPEAT_AFTER_USAGE_LENGTH)
315 {
316 /* Usage was very long, repeat the error message. */
317 RTStrmPutCh(g_pStdErr, '\n');
318 RTMsgErrorV(pszFormat, va);
319 }
320 return RTEXITCODE_SYNTAX;
321}
322
323
324/**
325 * Display current (sub)command usage and the custom error message.
326 *
327 * @returns RTEXITCODE_SYNTAX.
328 * @param pszFormat Custom error message format string.
329 * @param ... Format arguments.
330 */
331RTEXITCODE errorSyntax(const char *pszFormat, ...)
332{
333 va_list va;
334 va_start(va, pszFormat);
335 RTEXITCODE rcExit = errorSyntaxV(pszFormat, va);
336 va_end(va);
337 return rcExit;
338}
339
340
341/**
342 * Display current (sub)command usage and the custom error message.
343 *
344 * @returns E_INVALIDARG
345 * @param pszFormat Custom error message format string.
346 * @param ... Format arguments.
347 */
348HRESULT errorSyntaxHr(const char *pszFormat, ...)
349{
350 va_list va;
351 va_start(va, pszFormat);
352 errorSyntaxV(pszFormat, va);
353 va_end(va);
354 return E_INVALIDARG;
355}
356
357
358/**
359 * Print an error message without the syntax stuff.
360 *
361 * @returns RTEXITCODE_SYNTAX.
362 */
363RTEXITCODE errorArgument(const char *pszFormat, ...)
364{
365 va_list args;
366 va_start(args, pszFormat);
367 RTMsgErrorV(pszFormat, args);
368 va_end(args);
369 return RTEXITCODE_SYNTAX;
370}
371
372
373/**
374 * Print an error message without the syntax stuff.
375 *
376 * @returns E_INVALIDARG.
377 */
378HRESULT errorArgumentHr(const char *pszFormat, ...)
379{
380 va_list args;
381 va_start(args, pszFormat);
382 RTMsgErrorV(pszFormat, args);
383 va_end(args);
384 return E_INVALIDARG;
385}
386
387
388/**
389 * Worker for errorGetOpt.
390 *
391 * @param rcGetOpt The RTGetOpt return value.
392 * @param pValueUnion The value union returned by RTGetOpt.
393 */
394static void errorGetOptWorker(int rcGetOpt, union RTGETOPTUNION const *pValueUnion)
395{
396 if (rcGetOpt == VINF_GETOPT_NOT_OPTION)
397 RTMsgError(Help::tr("Invalid parameter '%s'"), pValueUnion->psz);
398 else if (rcGetOpt > 0)
399 {
400 if (RT_C_IS_PRINT(rcGetOpt))
401 RTMsgError(Help::tr("Invalid option -%c"), rcGetOpt);
402 else
403 RTMsgError(Help::tr("Invalid option case %i"), rcGetOpt);
404 }
405 else if (rcGetOpt == VERR_GETOPT_UNKNOWN_OPTION)
406 RTMsgError(Help::tr("Unknown option: %s"), pValueUnion->psz);
407 else if (rcGetOpt == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
408 RTMsgError(Help::tr("Invalid argument format: %s"), pValueUnion->psz);
409 else if (pValueUnion->pDef)
410 RTMsgError("%s: %Rrs", pValueUnion->pDef->pszLong, rcGetOpt);
411 else
412 RTMsgError("%Rrs", rcGetOpt);
413}
414
415
416/**
417 * For use to deal with RTGetOptFetchValue failures.
418 *
419 * @retval RTEXITCODE_SYNTAX
420 * @param iValueNo The value number being fetched, counting the
421 * RTGetOpt value as zero and the first
422 * RTGetOptFetchValue call as one.
423 * @param pszOption The option being parsed.
424 * @param rcGetOptFetchValue The status returned by RTGetOptFetchValue.
425 * @param pValueUnion The value union returned by the fetch.
426 */
427RTEXITCODE errorFetchValue(int iValueNo, const char *pszOption, int rcGetOptFetchValue, union RTGETOPTUNION const *pValueUnion)
428{
429 Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
430 showLogo(g_pStdErr);
431 if (rcGetOptFetchValue == VERR_GETOPT_REQUIRED_ARGUMENT_MISSING)
432 RTMsgError(Help::tr("Missing the %u%s value for option %s"),
433 iValueNo,
434 iValueNo == 1 ? Help::tr("st")
435 : iValueNo == 2 ? Help::tr("nd")
436 : iValueNo == 3 ? Help::tr("rd")
437 : Help::tr("th"),
438 pszOption);
439 else
440 errorGetOptWorker(rcGetOptFetchValue, pValueUnion);
441 return RTEXITCODE_SYNTAX;
442
443}
444
445
446/**
447 * Handled an RTGetOpt error or common option.
448 *
449 * This implements the 'V' and 'h' cases. It reports appropriate syntax errors
450 * for other @a rcGetOpt values.
451 *
452 * @retval RTEXITCODE_SUCCESS if help or version request.
453 * @retval RTEXITCODE_SYNTAX if not help or version request.
454 * @param rcGetOpt The RTGetOpt return value.
455 * @param pValueUnion The value union returned by RTGetOpt.
456 */
457RTEXITCODE errorGetOpt(int rcGetOpt, union RTGETOPTUNION const *pValueUnion)
458{
459 Assert(g_enmCurCommand != HELP_CMD_VBOXMANAGE_INVALID);
460
461 /*
462 * Check if it is an unhandled standard option.
463 */
464 if (rcGetOpt == 'V')
465 {
466 RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
467 return RTEXITCODE_SUCCESS;
468 }
469
470 if (rcGetOpt == 'h')
471 {
472 printFullCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdOut);
473 return RTEXITCODE_SUCCESS;
474 }
475
476 /*
477 * We failed.
478 */
479 showLogo(g_pStdErr);
480 errorGetOptWorker(rcGetOpt, pValueUnion);
481 if ( printBriefCommandOrSubcommandHelp(g_enmCurCommand, g_fCurSubcommandScope, g_pStdErr)
482 >= ERROR_REPEAT_AFTER_USAGE_LENGTH)
483 {
484 /* Usage was very long, repeat the error message. */
485 RTStrmPutCh(g_pStdErr, '\n');
486 errorGetOptWorker(rcGetOpt, pValueUnion);
487 }
488 return RTEXITCODE_SYNTAX;
489}
490
491#endif /* !VBOX_ONLY_DOCS */
492
493
494
495void showLogo(PRTSTREAM pStrm)
496{
497 static bool s_fShown; /* show only once */
498
499 if (!s_fShown)
500 {
501 RTStrmPrintf(pStrm, VBOX_PRODUCT " Command Line Management Interface Version "
502 VBOX_VERSION_STRING "\n"
503 "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
504 "All rights reserved.\n"
505 "\n");
506 s_fShown = true;
507 }
508}
509
510
511
512
513void printUsage(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, PRTSTREAM pStrm)
514{
515 bool fDumpOpts = false;
516#ifdef RT_OS_LINUX
517 bool fLinux = true;
518#else
519 bool fLinux = false;
520#endif
521#ifdef RT_OS_WINDOWS
522 bool fWin = true;
523#else
524 bool fWin = false;
525#endif
526#ifdef RT_OS_SOLARIS
527 bool fSolaris = true;
528#else
529 bool fSolaris = false;
530#endif
531#ifdef RT_OS_FREEBSD
532 bool fFreeBSD = true;
533#else
534 bool fFreeBSD = false;
535#endif
536#ifdef RT_OS_DARWIN
537 bool fDarwin = true;
538#else
539 bool fDarwin = false;
540#endif
541#ifdef VBOX_WITH_VBOXSDL
542 bool fVBoxSDL = true;
543#else
544 bool fVBoxSDL = false;
545#endif
546
547 RT_NOREF(fSubcommandScope);
548
549 Assert(enmCommand != USAGE_INVALID);
550 Assert(enmCommand != USAGE_S_NEWCMD);
551
552 if (enmCommand == USAGE_S_DUMPOPTS)
553 {
554 fDumpOpts = true;
555 fLinux = true;
556 fWin = true;
557 fSolaris = true;
558 fFreeBSD = true;
559 fDarwin = true;
560 fVBoxSDL = true;
561 enmCommand = USAGE_S_ALL;
562 }
563
564 RTStrmPrintf(pStrm,
565 Help::tr("Usage:\n"
566 "\n"));
567
568 if (enmCommand == USAGE_S_ALL)
569 RTStrmPrintf(pStrm,
570 " VBoxManage [<general option>] <command>\n"
571 "\n"
572 "\n"
573 "General Options:\n"
574 "\n"
575 " [-V|--version] print version number and exit\n"
576 " [--dump-build-type] print build type and exit\n"
577 " [-q|--nologo] suppress the logo\n"
578 " [--settingspw <pw>] provide the settings password\n"
579 " [--settingspwfile <file>] provide a file containing the settings password\n"
580 " [@<response-file>] load arguments from the given response file (bourne style)\n"
581 "\n"
582 "\n"
583 "Commands:\n"
584 "\n");
585
586 const char *pcszSep1 = " ";
587 const char *pcszSep2 = " ";
588 if (enmCommand != USAGE_S_ALL)
589 {
590 pcszSep1 = "VBoxManage";
591 pcszSep2 = "";
592 }
593
594#define SEP pcszSep1, pcszSep2
595
596 if (enmCommand == USAGE_STORAGEATTACH || enmCommand == USAGE_S_ALL)
597 RTStrmPrintf(pStrm,
598 "%s storageattach %s <uuid|vmname>\n"
599 " --storagectl <name>\n"
600 " [--port <number>]\n"
601 " [--device <number>]\n"
602 " [--type dvddrive|hdd|fdd]\n"
603 " [--medium none|emptydrive|additions|\n"
604 " <uuid|filename>|host:<drive>|iscsi]\n"
605 " [--mtype normal|writethrough|immutable|shareable|\n"
606 " readonly|multiattach]\n"
607 " [--comment <text>]\n"
608 " [--setuuid <uuid>]\n"
609 " [--setparentuuid <uuid>]\n"
610 " [--passthrough on|off]\n"
611 " [--tempeject on|off]\n"
612 " [--nonrotational on|off]\n"
613 " [--discard on|off]\n"
614 " [--hotpluggable on|off]\n"
615 " [--bandwidthgroup <name>]\n"
616 " [--forceunmount]\n"
617 " [--server <name>|<ip>]\n"
618 " [--target <target>]\n"
619 " [--tport <port>]\n"
620 " [--lun <lun>]\n"
621 " [--encodedlun <lun>]\n"
622 " [--username <username>]\n"
623 " [--password <password>]\n"
624 " [--passwordfile <file>]\n"
625 " [--initiator <initiator>]\n"
626 " [--intnet]\n"
627 "\n", SEP);
628
629 if (enmCommand == USAGE_MODIFYMEDIUM || enmCommand == USAGE_S_ALL)
630 RTStrmPrintf(pStrm,
631 "%s modifymedium %s [disk|dvd|floppy] <uuid|filename>\n"
632 " [--type normal|writethrough|immutable|shareable|\n"
633 " readonly|multiattach]\n"
634 " [--autoreset on|off]\n"
635 " [--property <name=[value]>]\n"
636 " [--compact]\n"
637 " [--resize <megabytes>|--resizebyte <bytes>]\n"
638 " [--move <path>]\n"
639 " [--setlocation <path>]\n"
640 " [--description <description string>]"
641 "\n", SEP);
642
643#if defined(VBOX_WITH_NAT_SERVICE)
644 if (enmCommand == USAGE_NATNETWORK || enmCommand == USAGE_S_ALL)
645 {
646 RTStrmPrintf(pStrm,
647 "%s natnetwork %s add --netname <name>\n"
648 " --network <network>\n"
649 " [--enable|--disable]\n"
650 " [--dhcp on|off]\n"
651 " [--port-forward-4 <rule>]\n"
652 " [--loopback-4 <rule>]\n"
653 " [--ipv6 on|off]\n"
654 " [--port-forward-6 <rule>]\n"
655 " [--loopback-6 <rule>]\n\n"
656 "%s natnetwork %s remove --netname <name>\n\n"
657 "%s natnetwork %s modify --netname <name>\n"
658 " [--network <network>]\n"
659 " [--enable|--disable]\n"
660 " [--dhcp on|off]\n"
661 " [--port-forward-4 <rule>]\n"
662 " [--loopback-4 <rule>]\n"
663 " [--ipv6 on|off]\n"
664 " [--port-forward-6 <rule>]\n"
665 " [--loopback-6 <rule>]\n\n"
666 "%s natnetwork %s start --netname <name>\n\n"
667 "%s natnetwork %s stop --netname <name>\n\n"
668 "%s natnetwork %s list [<pattern>]\n"
669 "\n", SEP, SEP, SEP, SEP, SEP, SEP);
670
671
672 }
673#endif
674
675#if defined(VBOX_WITH_NETFLT)
676 if (enmCommand == USAGE_HOSTONLYIFS || enmCommand == USAGE_S_ALL)
677 {
678 RTStrmPrintf(pStrm,
679 "%s hostonlyif %s ipconfig <name>\n"
680 " [--dhcp |\n"
681 " --ip <ipv4> [--netmask <ipv4> (def:255.255.255.0)]|\n"
682 " --ipv6 <ipv6> [--netmasklengthv6 <N> (def:64)]]", SEP);
683# if !defined(RT_OS_SOLARIS) || defined(VBOX_ONLY_DOCS)
684 RTStrmPrintf(pStrm,
685 " |\n"
686 " create |\n"
687 " remove <name>\n");
688# else
689 RTStrmPrintf(pStrm,
690 "\n");
691# endif
692 RTStrmPrintf(pStrm,
693 "\n");
694 }
695#endif
696
697 if (enmCommand == USAGE_USBDEVSOURCE || enmCommand == USAGE_S_ALL)
698 {
699 RTStrmPrintf(pStrm,
700 "%s usbdevsource %s add <source name>\n"
701 " --backend <backend>\n"
702 " --address <address>\n"
703 "%s usbdevsource %s remove <source name>\n"
704 "\n", SEP, SEP);
705 }
706
707#ifndef VBOX_ONLY_DOCS /* Converted to man page, not needed. */
708 if (enmCommand == USAGE_S_ALL)
709 {
710 uint32_t cPendingBlankLines = 0;
711 PCHELP_LANG_ENTRY_T pHelpLangEntry = ASMAtomicUoReadPtrT(&g_pHelpLangEntry, PCHELP_LANG_ENTRY_T);
712 uint32_t const cHelpEntries = *pHelpLangEntry->pcHelpEntries;
713 for (uint32_t i = 0; i < cHelpEntries; i++)
714 {
715 PCRTMSGREFENTRY pHelp = pHelpLangEntry->papHelpEntries[i];
716
717 while (cPendingBlankLines-- > 0)
718 RTStrmPutCh(pStrm, '\n');
719
720 char szFirstChar[8];
721 RTStrmPrintf(pStrm, " %s%s:\n", szFirstChar, captialize(pHelp->pszBrief, szFirstChar));
722
723 cPendingBlankLines = 0;
724 RTMsgRefEntryPrintStringTable(pStrm, &pHelp->Synopsis, RTMSGREFENTRYSTR_SCOPE_GLOBAL,
725 &cPendingBlankLines, NULL /*pcLinesWritten*/);
726 cPendingBlankLines = RT_MAX(cPendingBlankLines, 1);
727 }
728 }
729#endif
730}
731
732/**
733 * Print a usage synopsis and the syntax error message.
734 * @returns RTEXITCODE_SYNTAX.
735 */
736RTEXITCODE errorSyntax(USAGECATEGORY enmCommand, const char *pszFormat, ...)
737{
738 va_list args;
739 showLogo(g_pStdErr); // show logo even if suppressed
740#ifndef VBOX_ONLY_DOCS
741 if (g_fInternalMode)
742 printUsageInternal(enmCommand, g_pStdErr);
743 else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
744 printUsage(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, g_pStdErr);
745 else
746 printUsage(g_pStdErr);
747#else
748 RT_NOREF_PV(enmCommand);
749#endif
750 va_start(args, pszFormat);
751 RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
752 va_end(args);
753 return RTEXITCODE_SYNTAX;
754}
755
756/**
757 * Print a usage synopsis and the syntax error message.
758 * @returns RTEXITCODE_SYNTAX.
759 */
760RTEXITCODE errorSyntaxEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, const char *pszFormat, ...)
761{
762 va_list args;
763 showLogo(g_pStdErr); // show logo even if suppressed
764#ifndef VBOX_ONLY_DOCS
765 if (g_fInternalMode)
766 printUsageInternal(enmCommand, g_pStdErr);
767 else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
768 printUsage(enmCommand, fSubcommandScope, g_pStdErr);
769 else
770 printUsage(g_pStdErr);
771#else
772 RT_NOREF2(enmCommand, fSubcommandScope);
773#endif
774 va_start(args, pszFormat);
775 RTStrmPrintf(g_pStdErr, Help::tr("\nSyntax error: %N\n"), pszFormat, &args);
776 va_end(args);
777 return RTEXITCODE_SYNTAX;
778}
779
780/**
781 * errorSyntax for RTGetOpt users.
782 *
783 * @returns RTEXITCODE_SYNTAX.
784 *
785 * @param enmCommand The command.
786 * @param fSubcommandScope The subcommand scope, REFENTRYSTR_SCOPE_GLOBAL
787 * for all.
788 * @param rc The RTGetOpt return code.
789 * @param pValueUnion The value union.
790 */
791RTEXITCODE errorGetOptEx(USAGECATEGORY enmCommand, uint64_t fSubcommandScope, int rc, union RTGETOPTUNION const *pValueUnion)
792{
793 /*
794 * Check if it is an unhandled standard option.
795 */
796#ifndef VBOX_ONLY_DOCS
797 if (rc == 'V')
798 {
799 RTPrintf("%sr%d\n", VBOX_VERSION_STRING, RTBldCfgRevision());
800 return RTEXITCODE_SUCCESS;
801 }
802#endif
803
804 if (rc == 'h')
805 {
806 showLogo(g_pStdErr);
807#ifndef VBOX_ONLY_DOCS
808 if (g_fInternalMode)
809 printUsageInternal(enmCommand, g_pStdOut);
810 else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
811 printUsage(enmCommand, fSubcommandScope, g_pStdOut);
812 else
813 printUsage(g_pStdErr);
814#endif
815 return RTEXITCODE_SUCCESS;
816 }
817
818 /*
819 * General failure.
820 */
821 showLogo(g_pStdErr); // show logo even if suppressed
822#ifndef VBOX_ONLY_DOCS
823 if (g_fInternalMode)
824 printUsageInternal(enmCommand, g_pStdErr);
825 else if (g_enmCurCommand == HELP_CMD_VBOXMANAGE_INVALID)
826 printUsage(enmCommand, fSubcommandScope, g_pStdErr);
827 else
828 printUsage(g_pStdErr);
829#else
830 RT_NOREF2(enmCommand, fSubcommandScope);
831#endif
832
833 if (rc == VINF_GETOPT_NOT_OPTION)
834 return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid parameter '%s'"), pValueUnion->psz);
835 if (rc > 0)
836 {
837 if (RT_C_IS_PRINT(rc))
838 return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option -%c"), rc);
839 return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid option case %i"), rc);
840 }
841 if (rc == VERR_GETOPT_UNKNOWN_OPTION)
842 return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Unknown option: %s"), pValueUnion->psz);
843 if (rc == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
844 return RTMsgErrorExit(RTEXITCODE_SYNTAX, Help::tr("Invalid argument format: %s"), pValueUnion->psz);
845 if (pValueUnion->pDef)
846 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%s: %Rrs", pValueUnion->pDef->pszLong, rc);
847 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "%Rrs", rc);
848}
849
850
851/**
852 * errorSyntax for RTGetOpt users.
853 *
854 * @returns RTEXITCODE_SYNTAX.
855 *
856 * @param enmCommand The command.
857 * @param rc The RTGetOpt return code.
858 * @param pValueUnion The value union.
859 */
860RTEXITCODE errorGetOpt(USAGECATEGORY enmCommand, int rc, union RTGETOPTUNION const *pValueUnion)
861{
862 return errorGetOptEx(enmCommand, RTMSGREFENTRYSTR_SCOPE_GLOBAL, rc, pValueUnion);
863}
864
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette