46732

How to do OptionContext parsing on an instance?

Question:

I'm trying to get options parsing using OptionContext to work.

My code so far:

public class Options : GLib.Object { public string option_output = ""; public Options () { } public void parse (string args[]) throws OptionError { // string option_output; const OptionEntry[] options = { { "output", 'o', 0, OptionArg.FILENAME, ref option_output, "file name for encoded output (required);", "FILE" }, {null} }; var opt_context = new OptionContext ("- vpng2theora"); opt_context.set_help_enabled (true); opt_context.add_main_entries (options, null); unowned string[] temp_args = args; foreach (var arg in temp_args) { print ("arg: %s\n", arg); } opt_context.parse (ref temp_args); print (option_output); } } int main (string[] args) { Options opts = new Options (); opts.parse (args); return 0; }

As it stands this doesn't compile because:

error: Value must be constant

If I remove the const altogether:

OptionEntry[] options = { { "output", 'o', 0, OptionArg.FILENAME, ref option_output, "file name for encoded output (required);", "FILE" }, {null} };

The error is:

error: Expected array element, got array initializer list

The only way I can get around this problem is declaring the option_output as a static class field, but that defeats the purpose of instantiation.

Is there any way to have the OptionContext parsing work on an instance instead of a static class?

Answer1:

The following will work:

public class Options : GLib.Object { public string option_output = ""; public bool parse (ref unowned string[] args) { var options = new OptionEntry[2]; options[0] = { "output", 'o', 0, OptionArg.FILENAME, ref option_output, "file name for encoded output (required);", "FILE" }; options[1] = {null}; var opt_context = new OptionContext ("- vpng2theora"); opt_context.set_help_enabled (true); opt_context.add_main_entries (options, null); foreach (var arg in args) { print ("arg: %s\n", arg); } bool result= true; try { opt_context.parse (ref args); print( option_output ); } catch { result = false; } return result; } } int main (string[] args) { Options opts = new Options (); if (!opts.parse (ref args)) { return -1; } return 0; }

The options parsing will remove the optional arguments and then you can parse the required ones, such as the filenames to process. That's why I've kept the original args passed to the parse function. So you will get a cleaned set of args back, that you can then parse again for the required arguments. I found <a href="http://julipedia.meroh.net/2013/08/cli-design-putting-flags-to-good-use.html" rel="nofollow">"CLI design: Putting flags to good use"</a> a useful read.

options is an array of structs. I don't understand well enough why setting the size of the array is necessary, but it works for me.

Recommend

  • How to display international scripts in QLabels?
  • Convert from hex-encoded CLOB to BLOB in Oracle
  • php DOMDocument - manipulating and encoding
  • Google Player Game Service Error: Can't build the sample game
  • Titanium doesn't recognize Android SDK on Windows
  • Yii2 Login with database
  • Submit a form with jQuery / Javascript without ignoring “required” tag
  • Granting permissions to Azure Active Directory Web Application automatically
  • Many to Many in Linq using Dapper
  • Backward compatibility of Python 3.5 for external modules
  • How to validate a year I enter in textbox using jquery rule?
  • Laravel at least one field is required
  • LiveData is abstract android
  • Monotouch crashes with NullReferenceException on non nullable object
  • Access object instance inside an event handler
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • How to get links to open in the native browser in iOS Meteor apps?
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Does Mobilefirst provide a provision to access web services directly?
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Running a C# exe file
  • Symfony2: How to get request parameter
  • Google cloud sdk not working when python points python3
  • Why winpcap requires both .lib and .dll to run?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • using HTMLImports.whenReady not working in chrome
  • coudnt use logback because of log4j
  • LevelDB C iterator
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Does armcc optimizes non-volatile variables with -O0?
  • How can i traverse a binary tree from right to left in java?