/src/wrappers/glib/library/utilities/g_option_context.e

http://github.com/tybor/Liberty · Specman e · 393 lines · 117 code · 76 blank · 200 comment · 5 complexity · 2215e0522a396cee54d608aff2e1e541 MD5 · raw file

  1. indexing
  2. description: "."
  3. copyright: "[
  4. Copyright (C) 2007 Paolo Redaelli, Glib developers
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This library is distributed in the hopeOA that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA
  17. ]"
  18. class G_OPTION_CONTEXT
  19. -- A commandline option parser.
  20. -- The GOption commandline parser is intended to be a simpler
  21. -- replacement for the popt library. It supports short and long
  22. -- commandline options, as shown in the following example:
  23. -- testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb --
  24. -- file1 file2
  25. -- The example demonstrates a number of features of the GOption
  26. -- commandline parser
  27. -- o Options can be single letters, prefixed by a single
  28. -- dash. Multiple short options can be grouped behind a single
  29. -- dash.
  30. -- o Long options are prefixed by two consecutive dashes.
  31. -- o Options can have an extra argument, which can be a number, a
  32. -- string or a filename. For long options, the extra argument can
  33. -- be appended with an equals sign after the option name.
  34. -- o Non-option arguments are returned to the application as rest
  35. -- arguments.
  36. -- o An argument consisting solely of two dashes turns off further
  37. -- parsing, any remaining arguments (even those starting with a
  38. -- dash) are returned to the application as rest arguments.
  39. -- Another important feature of GOption is that it can
  40. -- automatically generate nicely formatted help output. Unless it
  41. -- is explicitly turned off with `set_help_enabled', GOption will
  42. -- recognize the "--help", "-?", "--help-all" and
  43. -- "--help-groupname" options (where groupname is the name of a
  44. -- G_OPTION_GROUP) and write a text similar to the one shown in the
  45. -- following example to stdout.
  46. -- Usage:
  47. -- testtreemodel [OPTION...] - test tree model performance
  48. -- Help Options:
  49. -- -?, --help Show help options
  50. -- --help-all Show all help options
  51. -- --help-gtk Show GTK+ Options
  52. -- Application Options:
  53. -- -r, --repeats=N Average over N repetitions
  54. -- -m, --max-size=M Test up to 2^M items
  55. -- --display=DISPLAY X display to use
  56. -- -v, --verbose Be verbose
  57. -- -b, --beep Beep when done
  58. -- --rand Randomize the data
  59. -- GOption groups options in G_OPTION_GROUPs, which makes it easy to
  60. -- incorporate options from multiple sources. The intended use for
  61. -- this is to let applications collect option groups from the
  62. -- libraries it uses, add them to their G_OPTION_CONTEXT, and parse
  63. -- all options by a single call to `parse'. GTK provides its own
  64. -- group in GTK.option_group.
  65. -- If an option is declared to be of type string or filename,
  66. -- GOption takes care of converting it to the right encoding;
  67. -- strings are returned in UTF-8, filenames are returned in the
  68. -- GLib filename encoding.
  69. -- "commandline_parser_example" is a complete example of setting up
  70. -- GOption to parse the example commandline above and produce the
  71. -- example help output.
  72. inherit
  73. C_STRUCT redefine free end
  74. EIFFEL_OWNED redefine free end
  75. insert
  76. SHARED_G_ERROR
  77. ARGUMENTS undefine is_equal, copy end
  78. GOPTION_CONTEXT_STRUCT
  79. GOPTION_EXTERNALS
  80. creation with,make, from_external_pointer
  81. feature {} -- Creation
  82. with, make (a_parameter: ABSTRACT_STRING) is
  83. -- Creates a new option context.
  84. -- The parameter_string can serve multiple purposes. It can be used to
  85. -- add descriptions for "rest" arguments, which are not parsed by the
  86. -- GOptionContext, typically something like "FILES" or "FILE1
  87. -- FILE2...". If you are using G_OPTION_REMAINING for collecting "rest"
  88. -- arguments, GLib handles this automatically by using the
  89. -- arg_description of the corresponding GOptionEntry in the usage
  90. -- summary.
  91. -- Another usage is to give a short summary of the program
  92. -- functionality, like " - frob the strings", which will be displayed
  93. -- in the same line as the usage. For a longer description of the
  94. -- program functionality that should be displayed as a paragraph below
  95. -- the usage line, use g_option_context_set_summary().
  96. -- Note that the parameter_string is translated (see
  97. -- g_option_context_set_translate_func()).
  98. -- `a_parameter' : a string which is displayed in the first line of
  99. -- --help output, after the usage summary programname [OPTION...]
  100. require parameter_not_void: a_parameter/=Void
  101. do
  102. from_external_pointer(g_option_context_new(a_parameter.to_external))
  103. end
  104. feature
  105. set_summary (a_summary: ABSTRACT_STRING) is
  106. -- Adds a string to be displayed in --help output before the list of
  107. -- options. This is typically a summary of the program functionality.
  108. -- Note that the summary is translated (see `set_translate_func').
  109. -- `a_summary': a string to be shown in --help output before the list
  110. -- of options, or Void
  111. local sp: POINTER
  112. do
  113. if a_summary/=Void then sp:=a_summary.to_external end
  114. g_option_context_set_summary(handle,sp)
  115. end
  116. summary: FIXED_STRING is
  117. local sp: POINTER
  118. do
  119. sp:=g_option_context_get_summary(handle)
  120. if sp.is_not_null then create Result.from_external(sp) end
  121. end
  122. set_description (a_description: ABSTRACT_STRING) is
  123. -- Adds `a_description' string to be displayed in --help output after
  124. -- the list of options. This text often includes a bug reporting
  125. -- address.
  126. -- Note that the summary is translated (see set_translator).
  127. -- `a_description' may be Void to unset.
  128. do
  129. g_option_context_set_description(handle,null_or_string(a_description))
  130. ensure set: description.is_equal(a_description)
  131. end
  132. description: FIXED_STRING is
  133. -- The description set with `set_description'. May be Void
  134. local p: POINTER
  135. do
  136. p:=g_option_context_get_description(handle)
  137. if p.is_not_null
  138. then create Result.from_external(p)
  139. end
  140. end
  141. -- GTranslateFunc ()
  142. -- const gchar* (*GTranslateFunc) (const gchar *str,
  143. -- gpointer data);
  144. -- The type of functions which are used to translate user-visible strings, for
  145. -- --help output.
  146. -- str : the untranslated string
  147. -- data : user data specified when installing the function, e.g. in
  148. -- g_option_group_set_translate_func()
  149. -- Returns : a translation of the string for the current locale. The returned string
  150. -- is owned by GLib and must not be freed.
  151. -- ---------------------------------------------------------------------------------
  152. -- TODO: set_translator wrapping
  153. -- g_option_context_set_translate_func ()
  154. -- void g_option_context_set_translate_func
  155. -- (GOptionContext *context,
  156. -- GTranslateFunc func,
  157. -- gpointer data,
  158. -- GDestroyNotify destroy_notify);
  159. -- Sets the function which is used to translate the contexts user-visible strings,
  160. -- for --help output. If func is NULL, strings are not translated.
  161. -- Note that option groups have their own translation functions, this function only
  162. -- affects the parameter_string (see g_option_context_nex()), the summary (see
  163. -- g_option_context_set_summary()) and the description (see
  164. -- g_option_context_set_description()).
  165. -- If you are using gettext(), you only need to set the translation domain, see
  166. -- g_context_group_set_translation_domain().
  167. -- context : a GOptionContext
  168. -- func : the GTranslateFunc, or NULL
  169. -- data : user data to pass to func, or NULL
  170. -- destroy_notify : a function which gets called to free data, or NULL
  171. -- Since 2.12
  172. -- ---------------------------------------------------------------------------------
  173. -- g_option_context_set_translation_domain ()
  174. -- void g_option_context_set_translation_domain
  175. -- (GOptionContext *context,
  176. -- const gchar *domain);
  177. -- A convenience function to use gettext() for translating user-visible strings.
  178. -- context : a GOptionContext
  179. -- domain : the domain to use
  180. -- Since 2.12
  181. parse is
  182. -- Parses the command line arguments, recognizing options
  183. -- which have been added to context. This feature also invoke
  184. -- `g_set_prgname'.
  185. -- If the parsing is successful, any parsed arguments are
  186. -- removed from the array and argc and argv are updated
  187. -- accordingly. A '--' option is stripped from argv unless
  188. -- there are unparsed options before and after it, or some of
  189. -- the options after it start with '-'. In case of an error,
  190. -- argc and argv are left unmodified.
  191. -- If automatic --help support is enabled (see
  192. -- `set_help_enabled'), and the argv array contains one of
  193. -- the recognized help options, this function will produce
  194. -- help output to stdout and call exit (0).
  195. -- `is_successful' is set to True if the parsing was successful, FALSE
  196. -- if an error occurred
  197. local i, argc: INTEGER; -- arguments: FAST_ARRAY[STRING];
  198. argv: POINTER
  199. do
  200. -- context : a GOptionContext
  201. -- argc : a pointer to the number of command line arguments.
  202. -- argv : a pointer to the array of command line arguments.
  203. -- error : a return location for errors
  204. -- Note: the next two assignments are necessary to generate
  205. -- correct C code because command_arguments.to_external is
  206. -- actually implemented as a function therefore it cannot be
  207. -- used as an argument of address_of. AFAIK there is no way
  208. -- to add a precondition to address_or requiring that its
  209. -- argument is not the result of a function. Paolo 2007-04-23
  210. argc := se_argc
  211. argv := command_arguments.to_external
  212. is_successful := (g_option_context_parse
  213. (handle, $argc, -- i.e.: gint *argc,
  214. $argv, -- i.e.: -- gchar ***argv
  215. error.handle)).to_boolean
  216. -- Commandline arguments have been changed. We must
  217. -- rebuild `command_arguments' array.
  218. -- from i:=argc; command_arguments.clear_count; until i = 0
  219. -- loop i := i - 1 command_arguments.force(arguments.item(i),
  220. -- i) end
  221. ensure arguments_can_be_decreased: command_arguments.count <= old command_arguments.count
  222. end
  223. set_help_enabled (a_setting: BOOLEAN) is
  224. -- Enables or disables automatic generation of --help output. By
  225. -- default, `parse' recognizes "--help", "-?", "--help-all" and
  226. -- "--help-groupname" and creates suitable output to stdout.
  227. do
  228. g_option_context_set_help_enabled(handle,a_setting.to_integer)
  229. ensure set: a_setting = is_help_enabled
  230. end
  231. is_help_enabled: BOOLEAN is
  232. -- Is automatic "--help" generation turned on?
  233. do
  234. Result:=g_option_context_get_help_enabled(handle).to_boolean
  235. end
  236. set_ignore_unknown_options (a_setting: BOOLEAN) is
  237. -- Sets whether to ignore unknown options or not. If an
  238. -- argument is ignored, it is left in the `command_arguments'
  239. -- array after parsing. By default, `parse' treats unknown
  240. -- options as error.
  241. -- This setting does not affect non-option arguments
  242. -- (i.e. arguments which don't start with a dash). But note
  243. -- that GOption cannot reliably determine whether a
  244. -- non-option belongs to a preceding unknown option.
  245. -- `a_setting' : TRUE to ignore unknown options, FALSE to
  246. -- produce an error when unknown options are met
  247. do
  248. g_option_context_set_ignore_unknown_options(handle, a_setting.to_integer)
  249. end
  250. are_unknown_options_ignored: BOOLEAN is
  251. -- Are unknown options ignored?
  252. do
  253. Result:=g_option_context_get_ignore_unknown_options(handle).to_boolean
  254. end
  255. feature -- group options
  256. add_main_entries (some_entries: COLLECTION[G_OPTION_ENTRY];
  257. a_translaction_domain: STRING) is
  258. -- A convenience feature which creates a main group if it doesn't
  259. -- exist, adds the entries to it and sets the translation domain.
  260. -- `a_translation_domain': a translation domain to use for translating
  261. -- the --help output for the options in entries with gettext(), or
  262. -- Void.
  263. -- Performance: the wrapper is O(1) if some_entries is a
  264. -- NULL_TERMINATED_C_ARRAY, otherwise it has an
  265. -- O(some_entries.count) overhead, since it converts the
  266. -- collection into a NULL_TERMINATED_C_ARRAY
  267. require entries_not_void: some_entries /= Void
  268. local entries_array: NULL_TERMINATED_C_ARRAY[G_OPTION_ENTRY]
  269. do
  270. not_yet_implemented
  271. -- debug print ("G_OPTION_CONTEXT.add_main_entries%N") end
  272. -- if entries_array ?:= some_entries then
  273. -- entries_array ::= some_entries
  274. -- else create entries_array.from_collection(some_entries)
  275. -- end
  276. -- debug print (" entries array created:%N") end
  277. --debug entries_array.print_on(std_output) std_output.put_new_line end
  278. --g_option_context_add_main_entries
  279. --(handle, entries_array.storage.to_external,
  280. -- null_or_string(a_translaction_domain))
  281. end
  282. main_group: G_OPTION_GROUP is
  283. -- the main group of context, or Void if context doesn't have a main
  284. -- group.
  285. local p: POINTER
  286. do
  287. create Result.from_external_pointer(g_option_context_get_main_group(handle))
  288. -- Note that group belongs to context and should not be modified
  289. -- or freed.
  290. end
  291. add_group (a_group: G_OPTION_GROUP) is
  292. -- Adds a GOptionGroup to the context, so that parsing with context
  293. -- will recognize the options in the group.
  294. require group_not_void: a_group /= Void
  295. do
  296. g_option_context_add_group (handle,a_group.handle)
  297. -- Note that the added group will be freed together with the context
  298. -- when g_option_context_free() is called, so you must not free the
  299. -- group yourself after adding it to a context.
  300. --a_group.set_shared -- avoid freeing the underlying C structure.
  301. end
  302. set_main_group (a_group: G_OPTION_GROUP) is
  303. -- Sets a GOptionGroup as main group of the context. This has the same
  304. -- effect as calling `add_group', the only difference is that the
  305. -- options in the main group are treated differently when generating
  306. -- "--help" output.
  307. require group_not_void: a_group /= Void
  308. do
  309. g_option_context_set_main_group (handle, a_group.handle)
  310. -- a_group.set_shared -- avoid freeing the underlying C structure.
  311. end
  312. feature {}
  313. free (a_ptr: POINTER) is
  314. do
  315. g_option_context_free(a_ptr)
  316. end
  317. end -- class G_OPTION_CONTEXT