blob: d2145ad6e2d89a4f14a0cefa6035dfe93e767c1b [file] [log] [blame]
Simon Glass70db4212012-02-15 15:51:16 -08001/*
2 * Code for setting up command line flags like `./u-boot --help`
3 *
4 * Copyright (c) 2011 The Chromium OS Authors.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#ifndef __SANDBOX_GETOPT_H
10#define __SANDBOX_GETOPT_H
11
12struct sandbox_state;
13
14/*
15 * Internal structure for storing details about the flag.
16 * Most people should not have to dig around in this as
17 * it only gets parsed by the core sandbox code. End
18 * consumer code should focus on the macros below and
19 * the callback function.
20 */
Simon Glass7b3efc62013-12-03 16:43:23 -070021struct sandbox_cmdline_option {
Simon Glass70db4212012-02-15 15:51:16 -080022 /* The long flag name: "help" for "--help" */
23 const char *flag;
24 /* The (optional) short flag name: "h" for "-h" */
25 int flag_short;
26 /* The help string shown to the user when processing --help */
27 const char *help;
28 /* Whether this flag takes an argument */
29 int has_arg;
30 /* Callback into the end consumer code with the option */
31 int (*callback)(struct sandbox_state *state, const char *opt);
32};
33
34/*
35 * Internal macro to expand the lower macros into the necessary
36 * magic junk that makes this all work.
37 */
Simon Glass7b3efc62013-12-03 16:43:23 -070038#define _SANDBOX_CMDLINE_OPT(f, s, ha, h) \
39 static struct sandbox_cmdline_option sandbox_cmdline_option_##f = { \
Simon Glass70db4212012-02-15 15:51:16 -080040 .flag = #f, \
41 .flag_short = s, \
42 .help = h, \
43 .has_arg = ha, \
Simon Glass7b3efc62013-12-03 16:43:23 -070044 .callback = sandbox_cmdline_cb_##f, \
Simon Glass70db4212012-02-15 15:51:16 -080045 }; \
46 /* Ppointer to the struct in a special section for the linker script */ \
Marek BehĂșn236f2ec2021-05-20 13:23:52 +020047 static __used __section(".u_boot_sandbox_getopt") \
Simon Glass7b3efc62013-12-03 16:43:23 -070048 struct sandbox_cmdline_option \
49 *sandbox_cmdline_option_##f##_ptr = \
50 &sandbox_cmdline_option_##f
Simon Glass70db4212012-02-15 15:51:16 -080051
52/**
53 * Macros for end code to declare new command line flags.
54 *
55 * @param f The long flag name e.g. help
56 * @param ha Does the flag have an argument e.g. 0/1
57 * @param h The help string displayed when showing --help
58 *
59 * This invocation:
Simon Glass7b3efc62013-12-03 16:43:23 -070060 * SANDBOX_CMDLINE_OPT(foo, 0, "The foo arg");
Simon Glass70db4212012-02-15 15:51:16 -080061 * Will create a new flag named "--foo" (no short option) that takes
62 * no argument. If the user specifies "--foo", then the callback func
Simon Glass7b3efc62013-12-03 16:43:23 -070063 * sandbox_cmdline_cb_foo() will automatically be called.
Simon Glass70db4212012-02-15 15:51:16 -080064 */
Simon Glass7b3efc62013-12-03 16:43:23 -070065#define SANDBOX_CMDLINE_OPT(f, ha, h) _SANDBOX_CMDLINE_OPT(f, 0, ha, h)
Simon Glass70db4212012-02-15 15:51:16 -080066/*
67 * Same as above, but @s is used to specify a short flag e.g.
Simon Glass7b3efc62013-12-03 16:43:23 -070068 * SANDBOX_CMDLINE_OPT(foo, 'f', 0, "The foo arg");
Simon Glass70db4212012-02-15 15:51:16 -080069 */
Simon Glass7b3efc62013-12-03 16:43:23 -070070#define SANDBOX_CMDLINE_OPT_SHORT(f, s, ha, h) _SANDBOX_CMDLINE_OPT(f, s, ha, h)
Simon Glass70db4212012-02-15 15:51:16 -080071
72#endif