How do i run WinUI3 App from Commandline with arguments - .net-6.0

I build an app like that one:
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
string[] cmdargs = Environment.GetCommandLineArgs();
... Map arguments to variables ...
... Do stuff ...
}
I'm using the framework dependend publishing without Click-Once.
I tested that app with a debug profile that contains two cmd args and it works perfectly. But if i'm trying to execute it with MyApp.exe arg1 arg2, the app starts inside the cmd but it looks, like it doesn't do anything.
Maybe i missed anything?

Related

How do I execute a Move script with the Aptos CLI?

Let's say I have a Move script like this:
script {
use std::signer;
use aptos_framework::aptos_account;
use aptos_framework::aptos_coin;
use aptos_framework::coin;
fun main(src: &signer, dest: address, desired_balance: u64) {
let src_addr = signer::address_of(src);
let balance = coin::balance<aptos_coin::AptosCoin>(src_addr);
if (balance < desired_balance) {
aptos_account::transfer(src, dest, desired_balance - balance);
};
addr::my_module::do_nothing();
}
}
This is calling functions on the aptos_coin.move module, which is deployed on chain. What it does isn't so important for this question, but in short, it checks that the balance of the destination account is less than desired_balance, and if so, tops it up to desired_balance.
Notice also how it calls a function in a Move module I've defined:
module addr::my_module {
public entry fun do_nothing() { }
}
Where do I put these files? Do I need a Move.toml? How do I run my script with the CLI?
Let's run through how to execute a Move script with a step by step example, this should answer all your questions.
Make a new directory to work from:
mkdir testing
cd testing
Setup the Aptos CLI:
aptos init
The CLI will ask you which network you want to work with (e.g. devnet, testnet, mainnet). It will also ask you for your private key (which looks like this: 0xf1adc8d01c1a890f17efc6b08f92179e6008d43026dd56b71e7b0d9b453536be), or it can generate a new one for you, as part of setting up your account.
From here, initialize a new Move project:
aptos move init --name my_script
Now you need to make a file for your script. So, take the script you created above, and put it in sources/, e.g. like this:
testing/
Move.toml
sources/
top_up.move
In other words, top_up.move should contain the script you included in the question.
Now do the same thing with the Move module, leaving you with this:
testing/
Move.toml
sources/
top_up.move
my_module.move
Now you can compile the script:
$ aptos move compile --named-addresses addr=81e2e2499407693c81fe65c86405ca70df529438339d9da7a6fc2520142b591e
Compiling, may take a little while to download git dependencies...
INCLUDING DEPENDENCY AptosFramework
INCLUDING DEPENDENCY AptosStdlib
INCLUDING DEPENDENCY MoveStdlib
BUILDING my_script
{
"Result": []
}
Note how I use the --named-addresses argument. This is necessary because in your code, you refer to this named address called addr. The compiler needs to know what this refers to. Instead of using this CLI argument, you could put something like this in your Move.toml:
[addresses]
addr = "b078d693856a65401d492f99ca0d6a29a0c5c0e371bc2521570a86e40d95f823"
Finally you can run the compiled script:
$ aptos move run-script --compiled-script-path build/my_script/bytecode_scripts/main.mv --args address:b078d693856a65401d492f99ca0d6a29a0c5c0e371bc2521570a86e40d95f823 --args u64:5
Do you want to submit a transaction for a range of [17000 - 25500] Octas at a gas unit price of 100 Octas? [yes/no] >
yes
{
"Result": {
"transaction_hash": "0x655f839a45c5f14ba92590c321f97c3c3f9aba334b9152e994fb715d5648db4b",
"gas_used": 178,
"gas_unit_price": 100,
"sender": "81e2e2499407693c81fe65c86405ca70df529438339d9da7a6fc2520142b591e",
"sequence_number": 53,
"success": true,
"timestamp_us": 1669811892262502,
"version": 370133122,
"vm_status": "Executed successfully"
}
}
Note that the path of the compiled script is under build/my_script/, not build/top_up/. This is because it uses the name of the project contained in Move.toml, which is my_script from when we ran aptos move init --name my_script.
So to answer one of your questions, yes you need a Move.toml, you can't currently just execute a script file on its own with the CLI. The compiler needs this determine what Aptos framework to use for example.
See the code used in this answer here: https://github.com/banool/move-examples/tree/main/run_script.
See also how to do this with the Rust SDK instead of the CLI: How do I execute a Move script on Aptos using the Rust SDK?.
P.S. There is a more streamlined way to execute a script. Instead of running aptos move compile and then aptos move run-script --compiled-script-path separately, you can just do this:
$ aptos move run-script --script-path sources/my_script.move --args address:b078d693856a65401d492f99ca0d6a29a0c5c0e371bc2521570a86e40d95f823 --args u64:5
This will do both steps with a single CLI command. Note however that there are some major footguns with this approach, see https://github.com/aptos-labs/aptos-core/issues/5733. So I'd recommend using the previous two-step approach for now.

How to call ruby class from Groovy class and execute it from groovy class

I wrote a ruby class which has print statement. Then i wrote a Groovy class which invokes this ruby class and executes
I tried like Process.execute("ruby.exe test.rb")
Ruby code-->
class Test
puts "hello, I am ruby"
end
Groovy code-->
class TestGroovy {
static main(String[] args) {
Process.execute("ruby.exe test.rb")
}
}
i need to get output as hello, I am ruby when i run TestGroovy.
From the docs:
Groovy provides a simple way to execute command line processes. Simply write the command line as a string and call the execute() method. E.g., on a *nix machine (or a windows machine with appropriate *nix commands installed), you can execute this:
def process = "ls -l".execute() // <1>
println "Found text ${process.text}" // <2>
executes the ls command in an external process
consume the output of the command and retrieve the tex
You can execute Ruby code from Groovy or Java code using a JSR-223 script engine. Here is an example Groovy script:
#Grab('org.jruby:jruby:9.2.5.0')
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
ScriptEngine engine = new ScriptEngineManager().getEngineByName('jruby')
engine.eval('puts "Hello world!"')
eval() also accepts a java.io.Reader which you could get from your file path. There are lots more details on ways to run Ruby from Java/Groovy here: https://github.com/jruby/jruby/wiki/RedBridge

Start cmd openvpn-gui with arguments in QT

I have a QT desktop aplication and now I want that when it starts it automatically calls openvpn to start too.
First try I've made it with a simple system() call, and it works, but it leaves cmd window in screen. I don't like it, but it works:
system("openvpn-gui --connect m2smart.ovpn")
But now I want to launch directly without needing the cmd window, and if I do it without arguments it works, this:
QProcess openvpn;
QString cmd("openvpn-gui.exe");
openvpn.startDetached(cmd);
Now, the only thing that I need is to indicate which config.ovpn to connect, like in the first example, I've searched how to do it and it seems to be like that:
QProcess openvpn;
QString cmd("openvpn-gui.exe");
QStringList args;
args << "--connect m2smart.ovpn";
openvpn.startDetached(cmd, args);
But it shows an openvpn window that says:
"Options error: unrecognized option or missing parameter(s): --connect m2smart.ovpn"
But if I open a cmd and put "openvpn-gui --connect m2smart.ovpn it works, independently of my working directory.
So... anyone can help me? I'm stucked with this.
Thanks!
First of all, QProcess::startDetached() is a static method. There is no need to instantiate a QProcess object before calling it. You just need to do:
QProcess::startDetached("openvpn-gui.exe");
Now, We have two overloads for QProcess::startDetached():
QProcess::startDetached(const QString& command):
There is no difference between this and using system function from the started program's point of view. So, on windows, if you had something like:
QProcess::startDetached("openvpn-gui.exe --connect m2smart.ovpn");
It would be the same as opening cmd.exe and typing openvpn-gui.exe --connect m2smart.ovpn.
QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = Q_NULLPTR):
From the docs:
On Windows, The arguments are quoted and joined into a command line that is compatible with the CommandLineToArgvW() Windows function.
So, If you used something like this in your program:
QString cmd("openvpn-gui.exe");
QStringList args;
args << "--connect m2smart.ovpn";
QProcess::startDetached(cmd, args);
This would be like opening cmd.exe and typing openvpn-gui.exe "--connect m2smart.ovpn".
In the first case, the first argument passed to openvpn-gui.exe is --connect and the second argument is m2smart.ovpn.
In the second case, there is only one argument that is --connect m2smart.ovpn.

What is the equivalent of an Ant taskdef in Gradle?

I've been struggling with this for a day and a half or so. I'm trying to replicate the following Ant concept in Gradle:
<target name="test">
...
<runexe name="<filename> params="<params>" />
...
</target>
where runexe is declared elsewhere as
<macrodef name="runexe" >
...
</macrodef>
and might also be a taskdef or a scriptdef i.e. I'd like to be able to call a reusable, pre-defined block of code and pass it the necessary parameters from within Gradle tasks. I've tried many things. I can create a task that runs the exe without any trouble:
task runexe(type: Exec){
commandLine 'cmd', '/c', 'dir', '/B'
}
task test(dependsOn: 'runexe') {
runexe {
commandLine 'cmd', '/c', 'dir', '/N', 'e:\\utilities\\'
}
}
test << {
println "Testing..."
// I want to call runexe here.
...
}
and use dependsOn to have it run. However this doesn't allow me to run runexe precisely when I need to. I've experimented extensively with executable, args and commandLine. I've played around with exec and tried several different variations found here and around the 'net. I've also been working with the free books available from the Gradle site.
What I need to do is read a list of files from a directory and pass each file to the application with some other arguments. The list of files won't be known until execution time i.e. until the script reads them, the list can vary and the call needs to be made repeatedly.
My best option currently appears to be what I found here, which may be fine, but it just seems that there should be a better way. I understand that tasks are meant to be called once and that you can't call a task from within another task or pass one parameters but I'd dearly like to know what the correct approach to this is in Gradle. I'm hoping that one of the Gradle designers might be kind enough to enlighten me as this is a question asked frequently all over the web and I'm yet to find a clear answer or a solution that I can make work.
If your task needs to read file names, then I suggest to use the provided API instead of executing commands. Also using exec will make it OS specific, therefore not necessarily portable on different OS.
Here's how to do it:
task hello {
doLast {
def tree = fileTree(dir: '/tmp/test/txt')
def array = []
tree.each {
array << it
print "${it.getName()} added to array!\n"
}
}
}
I ultimately went with this, mentioned above. I have exec {} working well in several places and it seems to be the best option for this use case.
To please an overzealous moderator, that means this:
def doMyThing(String target) {
exec {
executable "something.sh"
args "-t", target
}
}
as mentioned above. This provides the same ultimate functionality.

Running CakePHP Shell Script

I have created a shell script as follows
<?php
class EmailShell extends AppShell
{
public function main()
{
$this->out('Hello world.');
}
}
When i navigate to the Console folder in command line and type cake email i get the following error.
Error: Shell class EmailShell could not be found.
#0 C:\wamp\www\gitgrow\lib\Cake\Console\ShellDispatcher.php(167): ShellDispatche
r->_getShell('email')
#1 C:\wamp\www\gitgrow\lib\Cake\Console\ShellDispatcher.php(69): ShellDispatcher
->dispatch()
#2 C:\wamp\www\gitgrow\app\Console\cake.php(33): ShellDispatcher::run(Array)
#3 {main}
create a shell for use in the Console. For this example, we’ll create a simple Hello world shell. In you applications Console/Command directory create EmailShell.php. Put the following code inside it:
class EmailShell extends AppShell {
public function main() {
$this->out('Hello world.');
}
}
Then run this command :
Console/cake email
or
cake email
Run it at C:\wamp\www\gitgrow\app\. It should work.
cd C:\wamp\www\gitgrow\app
Console\cake email
If your shell class is in the right place, then it might be a problem that cake does not know where your app root is. You can specify this using the -app argument.
cake -app ../app email
See the following link about how to run Cake shells in cron:
http://book.cakephp.org/2.0/en/console-and-shells/cron-jobs.html
Your cron command basically calls cd into the app directory and the cake command to run the shell together.

Resources