getopt.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  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. #ifndef __SANDBOX_GETOPT_H
  9. #define __SANDBOX_GETOPT_H
  10. struct sandbox_state;
  11. /*
  12. * Internal structure for storing details about the flag.
  13. * Most people should not have to dig around in this as
  14. * it only gets parsed by the core sandbox code. End
  15. * consumer code should focus on the macros below and
  16. * the callback function.
  17. */
  18. struct sandbox_cmdline_option {
  19. /* The long flag name: "help" for "--help" */
  20. const char *flag;
  21. /* The (optional) short flag name: "h" for "-h" */
  22. int flag_short;
  23. /* The help string shown to the user when processing --help */
  24. const char *help;
  25. /* Whether this flag takes an argument */
  26. int has_arg;
  27. /* Callback into the end consumer code with the option */
  28. int (*callback)(struct sandbox_state *state, const char *opt);
  29. };
  30. /*
  31. * Internal macro to expand the lower macros into the necessary
  32. * magic junk that makes this all work.
  33. */
  34. #define _SANDBOX_CMDLINE_OPT(f, s, ha, h) \
  35. static struct sandbox_cmdline_option sandbox_cmdline_option_##f = { \
  36. .flag = #f, \
  37. .flag_short = s, \
  38. .help = h, \
  39. .has_arg = ha, \
  40. .callback = sandbox_cmdline_cb_##f, \
  41. }; \
  42. /* Ppointer to the struct in a special section for the linker script */ \
  43. static __used __section(".u_boot_sandbox_getopt") \
  44. struct sandbox_cmdline_option \
  45. *sandbox_cmdline_option_##f##_ptr = \
  46. &sandbox_cmdline_option_##f
  47. /**
  48. * Macros for end code to declare new command line flags.
  49. *
  50. * @param f The long flag name e.g. help
  51. * @param ha Does the flag have an argument e.g. 0/1
  52. * @param h The help string displayed when showing --help
  53. *
  54. * This invocation:
  55. * SANDBOX_CMDLINE_OPT(foo, 0, "The foo arg");
  56. * Will create a new flag named "--foo" (no short option) that takes
  57. * no argument. If the user specifies "--foo", then the callback func
  58. * sandbox_cmdline_cb_foo() will automatically be called.
  59. */
  60. #define SANDBOX_CMDLINE_OPT(f, ha, h) _SANDBOX_CMDLINE_OPT(f, 0, ha, h)
  61. /*
  62. * Same as above, but @s is used to specify a short flag e.g.
  63. * SANDBOX_CMDLINE_OPT(foo, 'f', 0, "The foo arg");
  64. */
  65. #define SANDBOX_CMDLINE_OPT_SHORT(f, s, ha, h) _SANDBOX_CMDLINE_OPT(f, s, ha, h)
  66. #endif