Extracting '--help' from a python script

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);
}