Is Groovy 2.2 broken for argument parsing with -n? - groovy

This is sort of obscure, but I can't figure it out.
I have a program that uses Groovy's CliBuilder to parse command line options. Here's a simplified version of the code:
args.each { println it }
def cli = new CliBuilder(usage: '...usage...')
cli.n(args: 1, 'The name')
def options = cli.parse(args)
println ":" + options.n
I run the program with:
groovy test.groovy -n name
Using Groovy 2.1.7, I get the expected response:
-n
name
:name
Using Groovy 2.2.2, I get something strange:
?
name
:false
This only happens when using -n as a command-line option. I don't know if it conflicts with Groovy's own -n option or not. It doesn't seem to in 2.1.7. Using -n also breaks any subsequent options passed on the same command line.
Is this a bug? Is the workaround just to avoid -n?

Related

Groovy script "unexpected token" with # directive

This has me totally baffled.
Specs: Linux Mint 18.3, Java 11, Groovy 2.5.9.
It may be of interest that these files are on an NTFS-formatted partition.
I make a simple Groovy script file with a simple #Grab:
package test;
#Grab(group='org.apache.commons', module='commons-lang3', version='3.7')
println "bye"
... fails:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/media/chris/W10 D drive/My Documents/software projects/EclipseWorkspace/GroovyExp2020-03-08/src/test/testscript.groovy: 5: unexpected token: println # line 5, column 1.
println "bye"
^
1 error
If I comment out the #Grab line it runs OK.
If I make the script like this:
package test
def bosh = "jellow world"
bosh = "bash"
#Grab(group='org.apache.commons', module='commons-lang3', version='3.7')
bosh = "bish"
... the complaint, flagged for the last line there, changes to "Groovy:The current scope already contains a variable of the name bosh" (!).
If I put a semicolon at the end of the Grab line:
package test;
#Grab(group='org.apache.commons', module='commons-lang3', version='3.7');
println "bye"
... the complaint becomes "Unexpected token:;"
So far so bafflingly inexplicable. Even stranger is the fact, however, that some of my existing scripts still work OK: I can insert a #Grab line like the above, with no complaints about "unexpected token".
This is not Eclipse-generated nonsense: I get the same issues at the Linux CLI.
You can't just stick annotations anywhere in groovy (or Java)
You need to annotate a class, method or field
In your examples, you're annotating a variable assignment (which won't work), and a method call (which also won't work)
Move the annotation to the top of the file, or to a class definition

Bash wrong spaces, quotes interpretation in variables

I see a weird behavior in shell scripts when I pass a variable with parameters to external ruby script
For example:
params="--val1=test --val2='test'"
ruby ./script.rb
causes ruby to output 'test' for var2 instead of test.
If I just pass params directly without using a variable everything works just fine.
Could you please clarify your question a bit?
From what I understand you have a shell script, something like:
#!/bin/bash
PARAMS="--val1=test --val2='test'"
ruby ./script.rb $PARAMS
And in script.rb you print out the value for the command line parameter val2. In this case it's expected that it prints out test instead of 'test', because the following steps are happening:
bash replaces $PARAMS with its value
bash tries to execute the line ruby ./script.rb --val1=test --val2='test'
now bash sees the quoted value 'test' and replaces it with test, so ruby / your script sees test

How do I make intellij Idea to highlight Scala script correctly with #! (shebang)

How do I make intellij Idea to highlight Scala script correctly.
Attempt 1
change filename to 'test.sc' . Intellij does not like the first line i.e it is not valid scala comment syntax
Attempt 2
change filename to 'test.sh' . Intellij thinks all of the syntax is bash script.
filenName = ./test.sh
#!/usr/bin/env amm
import ammonite.ops._ , ImplicitWd._
println("Stop script")
val x = 1 + 1
If you need to preserve .sc extension here is a little trick
change shebang to #! /usr/bin/env amm which is still a valid shebang
put a file named #!.scala next to your script with the content:
class CLZ {
def /(that: CLZ): CLZ = this
def amm: Unit = {}
}
object #! extends CLZ
object usr extends CLZ
object bin extends CLZ
object env extends CLZ
Now, your script is properly highlighted
Update
JetBrains published a blog article about support for Ammonite.
If you have a .scala extension, IDEA recognizes as a valid Scala script file.
Even a multiline shebang line is supported:
#!/bin/sh
DIR=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)
exec scala -classpath $DIR/scalaj-http_2.11-2.3.0.jar -savecompiled "$0" "$#"
!#
/* Scala code*/
Make sure:
you are using a relatively recent version of IDEA.
Scala plugin is installed.
The script lies in a Scala module.
It works even if the script is outside of a declared source folder.
I can even edit scripts outside of my project.

Pass a variable from Jython (wsadmin) to shell script

I am trying to pass a value retrieved from WebSphere using a Jython script called by wsadmin.sh to a variable in my caller shell script.
The caller shell script (getValue.sh) would have:
#!/bin/sh
/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py
exit 0
The Jython script (Jython.py) would have:
cellName = AdminControl.getCell()
return cellName
How can I store the value of cellName into a variable in my shell script, for example, CELL_NAME, that I could use like:
echo "Cell Name is: " ${CELL_NAME}
This version of the Jython script is a lot simpler than the one I am using in reality, but I think the concept is the same.
If I am using a lot of functions in the Jython script, is there a way to pass one of the values to my shell script? i.e.
def getValue1():
value1 = "1"
return value1
def getValue2():
value2 = "2"
return value2
def getValue3():
value3 = "3"
return value3
print getValue1()
print getValue2()
print getValue3()
I there a way to store multiple values into different shell script variables? i.e.
echo "Value #1: " ${VALUE_ONE}
echo "Value #2: " ${VALUE_TWO}
echo "Value #3: " ${VALUE_THREE}
... This way I could run one Jython script that would retrieve multiple values and use those multiple values in my shell script for further processing.
Thank you for any help you can provide.
Thank you Matt. You put me on the right track. I was able to accomplish what I needed by adding " | tail -1" to the command. You probably already know how a wsadmin SOAP connection always spits out the line:
WASX7209I: Connected to process "dmgr" on node labCellManager01 using SOAP connector; The type of process is: DeploymentManager
... so I had to find a way to take only the last part of the screen output to assign to my variable, thus using "tail -1".
The command becomes:
result=`/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py | tail -1`
Using that, you have to be careful about what you are printing on the screen in the Jython script, because only the last print will be assigned to the variable. You can adjust what you need with the tail command.
Thank you for your help
I will try to use your code as much as possible. I think the way you're going to find most effective is using the print command from Jython. if your get variable script looks like this
#!/bin/sh
/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py
exit 0
Then somewhere in your Jython.py file, you will need to print. It may look something like this.
def getValue1():
value1 = "1"
return value1
def getValue2():
value2 = "2"
return value2
def getValue3():
value3 = "3"
return value3
print getValue1()
print getValue2()
print getValue3()
output will be
1
2
3
if you need to do execute a command based on that, you could consider piping the results to xargs. Assuming above you could execute this instead
#!/bin/sh
/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py | xargs -i echo "hello world" > file{}.txt
exit 0
this will write "hello world" to file1.txt, file2.txt, and file3.txt
if you simply want to save the result, try
#!/bin/sh
result=`/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py`
exit 0
with ticks(`) (above tilde ~) surrounding your command.
My memory is a little fuzzy and you may need a quiet flag for wsadmin for those to work. I hope this helps.
Happy coding! Leave a comment if you have any more questions.

Why There Are Different Behaviors With Groovy Command Line Arguments?

I have a groovy file named test.groovy and have a single line of coding in it :
println args[0];
when I run this program like this groovy test ants, output is ants.
But when I run the program with the argument ants( then I'm getting error like this :
bash: syntax error near unexpected token (
1)If I escape the character ( then I'm getting the output as ants(. But why ( is needed to be escaped?
And when I run the program with the argument ant's, then clicking enter would make my terminal look like this :
>
>
>
2)And I terminate the program only using ctrl+c. What actually happens in this case? Why my terminal look like this?
3)After seeing these, what are the rules and condition that are to be followed in Groovy with accordance with Command-line arguments and the same holds for Java?
Thanks in advance.
You need to escape it as ( has a meaning in the bash shell which you are using.
The same goes for '
Try other commands:
ls (
Or
ls '
You'll get the same effect
Another option (other than escaping) is to put your arguments inside quote chars like so:
groovy test 'ants('

Resources