Extracting '--help' from a python script

Hello,

I would like to launch a python script from a button and pass command line arguments to the script. Currently I am using this code which works nicely:

cd {alias|python_scripts}
{alias|python}\python.exe -i dbug_printArgs.py {Rs|arguments}

showing this prompt to enter the command line options:
Clipboard%20Image%20(2)

But, instead of just showing 'arguments' in the command line prompt I would like to show the '--help' documentation of the file in the command prompt:
Clipboard%20Image

It should work something like this:

  • run {alias|python}\python.exe dbug_printArgs.py -h
  • save the manual as a string
  • run {alias|python}\python.exe -i dbug_printArgs.py {Rs|'put the saved manual string here'}

python mwe-script:

"""
my fancy python script
"""
import sys
import argparse

parser = argparse.ArgumentParser()
parser.description = __doc__
parser.epilog = '... example ...'
args = parser.parse_args()

print('number of arguments: ', len(sys.argv))
for idx, arg in enumerate(sys.argv):
	print(idx, arg)

dbug_printArgs.txt (297 Bytes)

Any Ideas whether this is possible at all? Where should I start studying the documentation?

Thank you very much for your help!

I have almost no idea what you want :frowning:

Maybe you can explain what this means:

I would like to retrieve the documentation string (manual / manpage / --help) from a python script and display it in a {Rs} dialog.

It should look like this:
Clipboard%20Image%20(3)

Change the commands at the top as appropriate.

(If you need unicode support for the help text, that's possible with a couple of small changes, but would mean you always had to run the command in a particular way, which might not be what you want. I'm assuming you don't need unicode for the help text, so left it like this so it is easier to change the commands.)

The script (usually) won't wait for the final command to finish, so if you need to do some other things after running the command it will need some slight changes. I assume you don't need that and wrote it this way as a result, but if you do, shout.

function OnClick(clickData)
{
	var helpCmd = 'cmd /c dir /?';
	var actualCmd = 'cmd /k dir';

	var cmd = clickData.func.command
	cmd.ClearFiles();

	var helpText = CaptureCommandOutput(cmd, helpCmd);
	var reqResult = RequestArgsFromUser(clickData, helpText);

	if (!reqResult[0])
		return;
	argsFromUser = reqResult[1];

	RunActualCommand(cmd, actualCmd, argsFromUser);
}

function CaptureCommandOutput(cmd, helpCmd)
{
	/*
	Alternative method is less code, but you can't
	stop the command prompt from flashing on the screen this way:
		var oShell = new ActiveXObject("WScript.Shell");
		var oExec = oShell.Exec(helpCmd);
		var strStdOut = oExec.StdOut.ReadAll();
		var strStdErr = oExec.StdErr.ReadAll();
		return strStdOut;
	*/

	var fs = new ActiveXObject('Scripting.FileSystemObject');
	var tempPath = '/temp/script_redir_' + fs.GetTempName();
	tempPath = DOpus.FSUtil.Resolve(tempPath);

	var cmdLine = '@sync:' + helpCmd  + '>"' + tempPath + '"';

	cmd.SetModifier("runmode", "hide");
//	DOpus.Output(cmdLine);
	cmd.RunCommand(cmdLine);
	cmd.ClearModifier("runmode");

	var helpFile = fs.OpenTextFile(tempPath, 1);
	var helpText = helpFile.ReadAll();
	helpFile.Close();

	cmdLine = 'Delete QUIET NORECYCLE FILE="' + tempPath + '"';
	cmd.RunCommand(cmdLine);

	return helpText;
}

function RequestArgsFromUser(clickData, helpText)
{
	var dlg = clickData.func.Dlg();
	dlg.message = helpText;
	dlg.title = 'My command args thing';
	dlg.buttons = '&OK|&Cancel';
	dlg.icon = 'question';
	dlg.max = 0;

	if (dlg.Show() != 1)
		return [false, ""];

	return [true, dlg.input];
}

function RunActualCommand(cmd, actualCmd, argsFromUser)
{
	var cmdLine = actualCmd;
	if (argsFromUser != "")
		cmdLine += ' ' + argsFromUser;

//	DOpus.Output(cmdLine);
	cmd.RunCommand(cmdLine);
}

Would that work for you too?

{alias|python}\python.exe -i dbug_printArgs.py | clip
{Rs|{clip}|example}
1 Like

That's not great if you were about to paste something into the dialog. :slight_smile:

The script I posted above leaves the clipboard alone.

Thank you very much to both of you for your help!

I tried Leo's solution, works like a charm:
Clipboard%20Image%20(4)

lxp's solution is as simple and elegant as I hoped it would be, unfortunately it prints any content of my clipboard at the time. I haven't figured out how to get the docstring in the clipboard.

{alias|python}\python.exe dbug_printArgs.py -h | clip
{Rs|{clip}|example}

Clipboard%20Image%20(5)

The | clip would normally do this, but apparently it doesn't pick up the output from python.exe.

It's because {clip} is evaluated and expanded before anything is run, not just before the line it's on is run.

(The clipboard is not a variable for scripting, even if a lot of people seem to think it is. :))

Out of curiosity, is there a way to define a variable without using the clipboard in a standard function script, much like lxp suggested?

...good to know!

This was my test button in which it worked:

dir d:\*.* | clip
@runbatch 
{RS|{clip}|example}

You can use @set for simple variables, but you'd usually be better off using a script to get real variables and flow control, unless you just want to define some paths or names and avoid repeating them on multiple lines, or similar.