Defining positional parameters with apache commons cli - apache-commons-cli

I would like to define an Apache Commons CLI parser that includes named arguments and positional arguments.
program [-a optA] [-b optB] [-f] pos1 pos2
How do I validate pos1 and pos2?

One a quick read of the documentation, I was not aware the the CommandLine class would provide access to the remaining positional parameters.
After parsing the Options passed on the command line, the remaining arguments are available in the CommandLine.getArgs() method.
public static void main(String[] args) {
DefaultParser clParse = new DefaultParser();
Options opts = new Options();
opts.addOption("a", true, "Option A");
opts.addOption("b", true, "Option B");
opts.addOption("f", false, "Flag F");
CommandLine cmdLine = clParse.parse(opts, args);
System.out.println(cmdLine.getArgs().length);
}

Related

How do I pass a docstring to pybind11::def_property_readonly?

I am trying to add a documented readonly property to a class in a python extension defined using pybind11. Usually this is done by adding a string argument to the define call. However, when I add a string argument to the readonly property definition call, I get template compile errors.
Compiles but doesn't have a docstring:
[...]
.def_property_readonly(
"property_name",
[](){ return ""; })
[...]
Has a docstring but doesn't compile:
[...]
.def_property_readonly(
"property_name",
[](){ return ""; },
std::string("docstring"))
[...]
You have to pass a const char * instead of a std::string.
[...]
.def_property_readonly(
"property_name",
[](){ return ""; },
"const char docstring")
[...]

Using PowerShell Parser for console application

My larger PowerShell/console application requires OOP, etc. for easier development.
I know about parsers such as NDesk.Options and CommandLineParser but I want something to simulate cmdlet parsing as close as possible. E.g. Support parsing for:
MyProject.exe do-thing ("computer1", "computer2") -p "parameter"
MyProject.exe do-thing -cn ("computer1", "computer2") -p "parameter" -verbose
Is it possible to use System.Management.Automation.Language.Parser or a another tool to simulate PowerShell parsing? Are there any examples and pitfalls to watch out for?
Sure, you could use Parser.ParseInput() to parse the args and then extract the individual elements from the resulting CommandAst:
using System.Management.Automation.Language;
public class Program
{
public static void Main(string[] args)
{
Token[] tokens;
ParseError[] errors;
Ast topAst = Parser.ParseInput(String.Join(" ", args), out tokens, out errors);
// Find the CommandAst object
CommandAst parsedCommand = topAst.Find(ast => ast is CommandAst, true);
// Grab the command name
string commandName = ((StringConstantExpressionAst)parsedCommand.CommandElements[0]).Value;
// Grab the remaining command arguments from CommandAst.CommandElements
}
}
I'd would probably store the arguments in a Dictionary<string,string[]>

Handling program args with Groovy Eclipse plugin v2

I am wondering how to handle program arguments when you are running Groovy within Eclipse. It isn't as straight forward as it is from the command line and I am having trouble figure it out. Im using Eclipse 3.5. My run configuration has these arguments all on one line:
--classpath "${workspace_loc:/GroovyProject};${workspace_loc:/GroovyProject}"
--main groovy.ui.GroovyMain "C:\Temp\Workspace\GroovyProject\GroovyTest.groovy "
argtest1
argtest2
argtest3
The script I am using to try to make this work looks like this:
// GroovyTest.groovy
class GroovyTest {
static main(args) {
println "hello, world"
for (arg in this.args ) {
println "Argument:" + arg;
}
}
}
The error I get is:
hello, world
Caught: groovy.lang.MissingPropertyException: No such property: args
for class: GroovyTest at GroovyTest.main(GroovyTest.groovy:5)
You have az unnecessary this in the for (arg in this.args) line.
this.args means that you have an instance of the GroovyTest object and you refer to its args field. In this case args is a method parameter so you have to refer to it simply as args.

output_from() function

i'm new to specman.
how do i use the output_from() function. and what does it do?
6.1.1 docs say:
25.8.4 output_from()
Purpose:
Collect the results of a system call
Category
Routine
Syntax :
output_from(command: string): list of string
Syntax Example
log_list = output_from("ls *log");
Parameter
command
A single operating system command, with or without parameters and enclosed in double quotes.
Description:
Executes the string as an operating system command and returns the output as a list of string. Under UNIX, stdout and stderr go to the string list.
Example
<'
extend sys {
m1() is {
var log_list: list of string;
log_list = output_from("ls *log");
print log_list;
};
};
'>

Why is param in this lambda expression?

The MSDN magazine article by Josh Smith on MVVM contains a lambda expression I don't completely understand. What is the purpose of param in this code?
_saveCommand = new RelayCommand(param => this.Save(),
param => this.CanSave );
Translated to my preferred language VB it's:
Dim saveAction as New Action(Of Object)(AddressOf Me.Save)
_saveCommand = New RelayCommand(saveAction, Function(param) Me.CanSave)
I would have expected to only see param if it is used within CanSave or Save. I am somewhat new to lambda expressions. It's odd for me to see a variable that is neither declared nor used anywhere as far as I can tell. Any explanation would be appreciated.
To put this in context the constructor for RelayCommand (C#) is:
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
and in VB:
Public Sub New(ByVal execute As Action(Of Object), _
ByVal canExecute As Predicate(Of Object))
The lambda expression is declaring it - the place where it appears is basically a declaration. If it didn't, it wouldn't be compatible with Action(Of Object). That's why it's there - even though you don't actually need the value.
With anonymous methods, if you don't need any parameter values you can omit the parameter list entirely:
_saveCommand = new RelayCommand(delegate { this.Save(); },
delegate { return this.CanSave; });
... but you can't do that with lambda expressions. You have to specify the parameter list - either just as a parameter name for a single parameter, or a full list in brackets. The code you've presented is equivalent to:
_saveCommand = new RelayCommand((Object param) => this.Save(),
(Object param) => this.CanSave);