如何对实例进行 OptionContext 解析?
How to do OptionContext parsing on an instance?
我正在尝试使用 OptionContext
来解析选项。
到目前为止我的代码:
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;
}
目前无法编译,因为:
error: Value must be constant
如果我完全删除 const
:
OptionEntry[] options = {
{ "output", 'o', 0, OptionArg.FILENAME,
ref option_output, "file name for encoded output (required);",
"FILE" },
{null}
};
错误是:
error: Expected array element, got array initializer list
解决这个问题的唯一方法是将 option_output
声明为 static
class 字段,但这违背了实例化的目的。
有什么方法可以让 OptionContext 解析在实例上而不是在静态上运行 class?
以下将起作用:
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;
}
选项解析将删除可选参数,然后您可以解析所需的参数,例如要处理的文件名。这就是为什么我将原始 args
传递给解析函数的原因。所以你会得到一组干净的 args
回来,然后你可以再次解析所需的参数。我发现 "CLI design: Putting flags to good use" 一本有用的书。
options
是一个结构数组。我不太明白为什么需要设置数组的大小,但它对我有用。
我正在尝试使用 OptionContext
来解析选项。
到目前为止我的代码:
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;
}
目前无法编译,因为:
error: Value must be constant
如果我完全删除 const
:
OptionEntry[] options = {
{ "output", 'o', 0, OptionArg.FILENAME,
ref option_output, "file name for encoded output (required);",
"FILE" },
{null}
};
错误是:
error: Expected array element, got array initializer list
解决这个问题的唯一方法是将 option_output
声明为 static
class 字段,但这违背了实例化的目的。
有什么方法可以让 OptionContext 解析在实例上而不是在静态上运行 class?
以下将起作用:
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;
}
选项解析将删除可选参数,然后您可以解析所需的参数,例如要处理的文件名。这就是为什么我将原始 args
传递给解析函数的原因。所以你会得到一组干净的 args
回来,然后你可以再次解析所需的参数。我发现 "CLI design: Putting flags to good use" 一本有用的书。
options
是一个结构数组。我不太明白为什么需要设置数组的大小,但它对我有用。