Apache Commons CLI Optional and positional argument - apache-commons

here's the deal : I want a flag "-pre" that if not filled will create a timestamp prefix ; and if filled, use the specified prefix.
Option prefixOption = OptionBuilder.create(O_PREFIX);
prefixOption.setLongOpt(O_PREFIX_LONG);
prefixOption.setArgName("prefix");
prefixOption.setDescription("Put a prefix (default is timestamp) to output directory.");
prefixOption.setType(String.class);
prefixOption.setOptionalArg(true);
And later..
System.out.println(commandLine.hasOption(O_PREFIX));
System.out.println(commandLine.getOptionValue(O_PREFIX));
Executions :
\> mon_execution -pre
true
null
\> mon_execution -pre Hahaha
true
null
and if I force the use of arg pre
prefixOption.setArgs(1); (in first snipet)
the command line take the next parameter as a parameter for -pre and breaks the parsing.
Any idea ?

Change in option declaration
Option prefixOption = new Option("pre", "prefix", true, "Put a prefix (default is timestamp) to output directory.");
prefixOption.setOptionalArg(true);
AND, most important, respect the order and put the positional optional argument at the end of the list.
Question following, as food for thought : how to use more than one of these positional optional option ? An other question for an other post - an other day.
Thanks.

Related

How to write an expression that evaluates a pipeline parameter to TRUE/FALSE?

I have a pipeline parameter fed by a config file in SQL.
Sometimes that parameter will be empty, not NULL but just empty ('').
How do I write an expression that will evaluate the parameter to TRUE/FALSE(blank/not blank) that I can put into my IF activity?
Basic question but thanks a lot.
I tried
#pipeline().parameters.x = ''
but it just told me Parameter x = '' was not found .......
You can use the below expression in the if activity to evaluate a parameter is empty or not.
#empty(pipeline().parameters.ok)
Sample demonstration:
A sample parameter ok.
For example, purpose I have created a string variable which I will use inside if to check the output.
In if give the above expression.
Inside True activities I have given a set variable activity and gave some value and did the same inside false activities.
Output when the parameter value is not given(empty).
Output when we gave any value to the parameter

Formatting string in Powershell but only first or specific occurrence of replacement token

I have a regular expression that I use several times in a script, where a single word gets changed but the rest of the expression remains the same. Normally I handle this by just creating a regular expression string with a format like the following example:
# Simple regex looking for exact string match
$regexTemplate = '^{0}$'
# Later on...
$someString = 'hello'
$someString -match ( $regexTemplate -f 'hello' ) # ==> True
However, I've written a more complex expression where I need to insert a variable into the expression template and... well regex syntax and string formatting syntax begin to clash:
$regexTemplate = '(?<=^\w{2}-){0}(?=-\d$)'
$awsRegion = 'us-east-1'
$subRegion = 'east'
$awsRegion -match ( $regexTemplate -f $subRegion ) # ==> Error
Which results in the following error:
InvalidOperation: Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
I know what the issue is, it's seeing one of my expression quantifiers as a replacement token. Rather than opt for a string-interpolation approach or replace {0} myself, is there a way I can tell PowerShell/.NET to only replace the 0-indexed token? Or is there another way to achieve the desired output using format strings?
If a string template includes { and/or } characters, you need to double these so they do not interfere with the numbered placeholders.
Try
$regexTemplate = '(?<=^\w{{2}}-){0}(?=-\d$)'

How can i remove a specific part of a variable value using perl

we have a variable values
value is : /abc/cde/efg/sanj/Sent//abc/cde/efg/sanj/sample sample1.pdf
I want to remove the middle part of this value '/abc/cde/efg/sanj/'.
Is it possible to remove this part.
Since we're talking paths,
use Path::Tiny qw( path );
my $qfn = '/abc/cde/efg/sanj/Sent//abc/cde/efg/sanj/sample sample1.pdf';
my $base_qfn = '/abc/cde/efg/sanj';
say path($qfn)->relative($base_qfn);
Output:
Sent/abc/cde/efg/sanj/sample sample1.pdf

Excel 2010 Slicers errors with parameter names

SlicersCaches.add and Slicers.Add gives error when I name the parameters but works fine without.
Error:
Set SC1 = ActiveWorkbook.SlicerCaches.Add(Source:=PivTable Sourcefield:="Dept")
Set SL1 = SC1.Slicers.Add(Slicerdestination:=PivSheet)
No Error:
Set SC1 = ActiveWorkbook.SlicerCaches.Add(PivTable, "Dept")
Set SL1 = SC1.Slicers.Add(PivSheet)
Is this a bug in the program?
There are no bugs in the program. When you call a Sub or Function procedure, you can supply arguments positionally, in the order they appear in the procedure's definition, or you can supply the arguments by name without regard to position.
Named arguments are especially useful when you are calling a procedure that has optional arguments. If you use named arguments, you don't have to include commas to denote missing positional arguments. Using named arguments makes it easier to keep track of which arguments you passed and which you omitted.
When you call a procedure with an optional argument, you can choose whether or not to specify the optional argument. If you don't specify the optional argument, the default value, if any, is used. If no default value is specified, the argument is it would be for any variable of the specified type.
Below are the complete definitions of the slicer functions with arguments (optional in square brackets):
Slicer.Add
Add(SlicerDestination, [Level], [Name], [Caption], [Top], [Left], [Width], [Height]) As Slicer
SlicerCache.Add
Add(Source, SourceField, [Name]) As SlicerCache

preg_match optional elements in string

I have two strings which contain up to 3 elements:
1) anychar[price]{alphanum} e.g. a1\')[=00.00]{a1234}
2) anychar:anychar{alphanum} e.g. a1\'):a2\'){a1234}
...but the {} element is optional and may not always be there. I wrote the following patterns (respectively):
1) /(.+)\[(.+)\]\{*(\w+)*\}*/ - works as expected
2) /(.+)\:(.+)\{*(\w+)*\}*/ - works fine if the {} element is removed, but not with it.
The result array for 2 is as follows:
(
[0] => a1\'):a2\'){a123}
[1] => a1\')
[2] => a2\'){a123}
)
I've tried a few different permutations of the above but no dice. Any ideas?
First you should remove the * after {, } and (\w+).
'/(.+)\:(.+)\{(\w+)\}/'
Gives
array(4) {
[0]=>
string(18) "a1\'):a2\'){a1234}"
[1]=>
string(5) "a1\')"
[2]=>
string(5) "a2\')"
[3]=>
string(5) "a1234"
}
* means either 0, 1 or several, and PCRE tries to find the quickest route it can, so if you make the whole third part optional (by using * everywhere) then the quickest route is to have everything included in the second group and skip the third, that's why your code didn't work.
Now in order to deal with the fact that the third part is optional, you have to use a positive lookahead: in the second group, you will ask pcre to select it only if it can matches another regex after it. The final regex is this:
'/(.+)\:(.+(?=(?:(?<=[^}])$|\{(\w+)\})))/'
What I changed is that:
inside the second group, i added a positive lookahead in the form (?=regex). As said, this means it has to match. Lookahead are not selective by default, which means that they don't create a entry in your final result/they are not returned to you.
inside that lookahead, I created two cases, which means that in order to match, the .+ from the second group will have to match either case of my lookahead.
The first case is very basic, it means end of string not preceded by a }, this will match the string when the 3rd part is not there
the second case if you selector for the 3rd group, we make it selectable so that it will be returned in the results if present

Resources