Author Topic: Passing variables via command line  (Read 9942 times)

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2831
Re: Passing variables via command line
« Reply #30 on: August 21, 2020, 06:51:36 PM »
@alterNERDtive - I have bolstered the command shared code a bit to try to correct the last exception you had indicated (this went out in the latest beta - v1.8.6.7).  Hoping that keeps things running - let me know otherwise, if you don't mind!

Robertsmania

  • Newbie
  • *
  • Posts: 46
Re: Passing variables via command line
« Reply #31 on: September 09, 2020, 05:18:18 PM »
I would very much like to get this to work.  I have been using the -command option to run commands from a python script and it would be great to include some more information as passed text.

I'm running the current beta v1.8.6.9 and can see it work calling one command from another.  So far I havent had any luck when calling from the command line.

I have a simple command: "test" which just logs ~passedText1 like so:
Write 'Passed text: '{TXT:~passedText1}'' to log

I've tried everything I can think of from the command line.  But no matter what, it always logs it out as 'Not set'
These are the command lines I've tried so far:

C:\Program Files (x86)\VoiceAttack>VoiceAttack.exe -command test -passedText asdf
C:\Program Files (x86)\VoiceAttack>VoiceAttack.exe -command test -passedText "asdf"
C:\Program Files (x86)\VoiceAttack>VoiceAttack.exe -command test -passedText \"asdf\"
C:\Program Files (x86)\VoiceAttack>VoiceAttack.exe -command test -passedText "\"asdf\""
C:\Program Files (x86)\VoiceAttack>VoiceAttack.exe -command test -passedText ""asdf""
C:\Program Files (x86)\VoiceAttack>VoiceAttack.exe -command test -passedText """asdf"""

It runs the test command every time, but I never get anything in ~passedText1.

4:12:06.222 Passed text: 'Not set'
4:12:06.218 External command : 'test'


I have another simple command "test1" which calls test and passes a text value: "asdf"
Execute command, 'test' - passing values (and wait until it completes)

That does work, when called that way test shows:
4:15:54.868 Passed text: 'asdf'
4:15:54.860 External command : 'test1'


Whats the magic syntax for the command line?



Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: Passing variables via command line
« Reply #32 on: September 09, 2020, 05:37:54 PM »
Are you using PowerShell? If so, you need to escape the inner double quotes using backticks, E.G.
Code: [Select]
-command test -passedText "\`"asdf\`""
This is how PowerShell works, and not specific to VoiceAttack (but, frankly, annoying if you're used to the relative simplicity of cmd).

Robertsmania

  • Newbie
  • *
  • Posts: 46
Re: Passing variables via command line
« Reply #33 on: September 09, 2020, 05:52:33 PM »
Thanks for the suggestion, but no I dont think I'm using power shell.  Just the command prompt - windows button: cmd.
Please see the attached image.

My actual goal is to get it to work from within a python script with the subprocess.call routine.  It wasnt working for me there, so I tried from the command prompt.  When it didnt work for me there either, I figured I would need to figure out how to get it to work manually first and then go back to the script.

Code: [Select]
va_binary = r"C:\Program Files (x86)\VoiceAttack\VoiceAttack.exe"

def test(data):
    cmd_test = "test"
    prm_test = "asdf"
    run_command_test(cmd_test, prm_test)
    return

def run_command_test(command_string, param_string):
    send_message(command_string)
    va_cmd = [settings["va_binary"], "-command", command_string, "-passedText", param_string]
    subprocess.call(va_cmd, shell=True)
    log(2, str(va_cmd))
    return

If I need to fumble with the quote/escaping within python thats fine, but I'd like to see it work manually from the console.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4781
  • RTFM
Re: Passing variables via command line
« Reply #34 on: September 09, 2020, 05:58:31 PM »
Code: [Select]
-command test -passedText "\"text here\""should work (as noted in the documentation, that is the correct syntax for literal text).

Are you running VoiceAttack as administrator? Currently passing values does not work when using the "Run VoiceAttack as an Administrator" option on the "System / Advanced" tab of the VoiceAttack options window, however it does work when using the "Run this program as an administrator" option on the compatibility tab of the properties of the executable.

Robertsmania

  • Newbie
  • *
  • Posts: 46
Re: Passing variables via command line
« Reply #35 on: September 09, 2020, 06:11:37 PM »
Yes, the issue was with running as administrator from the option in the configuration UI.

Turning that off, it works now both from the command line as well as from my python script.

Thank you for your help.

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2831
Re: Passing variables via command line
« Reply #36 on: September 09, 2020, 06:17:53 PM »
That's strange - the command is not run at all if VA is run as an administrator.  That is, the, 'test' command shouldn't be executed at all to be able to display, 'not set'.

On a side note, Windows is blocking in that case - you can run an elevated command prompt if you want to get past that.

Robertsmania

  • Newbie
  • *
  • Posts: 46
Re: Passing variables via command line
« Reply #37 on: September 09, 2020, 06:30:04 PM »
That does sound strange.  I run with a VoiceAttack plugin for iRacing called the Digital Race Engineer and that does prefer to have VA run as administrator.  When I saw the option in the UI that seemed better than setting it in the shortcut, so I enabled it.  But running like that, the command would run - both from the command prompt and from python but the passedText would not show up.

I followed the suggestion of turning the administrator option off, and the passedText started showing up provided the string was escaped as shown in the documentation:
Code: [Select]
VoiceAttack.exe -command test -passedText "\"text here\""
Then I tested it wiith the Run as administrator option enabled on the shortcut, and it seems to continue to work.

In my python, I do need to escape the quotes for the string similarly and this is what is working for me now:
Code: [Select]
va_binary = r"C:\Program Files (x86)\VoiceAttack\VoiceAttack.exe"

def test(data):
    cmd_test = "test"
    prm_test = "\"asdf\""
    run_command_test(cmd_test, prm_test)
    return


def run_command_test(command_string, param_string):
    va_cmd = [settings["va_binary"], "-command", command_string, "-passedText", param_string]
    subprocess.call(va_cmd, shell=True)
    log(2, str(va_cmd))
    return

And indeed when I look at the log output showing all the values in va_cmd it shows the string as "asdf".

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2831
Re: Passing variables via command line
« Reply #38 on: September 09, 2020, 10:01:39 PM »
I believe I've fixed the issue with the internally-set admin setting plus the, '-passedText' command line parameter.

Thanks for the heads up, @Robertsmania and thanks to @Pfeil for helping me pinpoint what was going on.