0

I have written a batch script that prompts the user for an input and displays the help for a specific library ExternalLibrary. However, I receive the error below the code sample.

@echo off
set input=NUL
set /p input="Help: "

python -c "import sys; sys.path.append('D:\\lib'); import ExternalLibrary;lookup = ExternalLibrary.Presentation_control();if ('%input%' == 'NUL'):&    help(lookup.%input%)&else:&    help(lookup)"

A working version can be achieved by replacing the if statement with:

help(lookup.%input%)

Error in the column that starts with if

C:\Users\usah>Lib_Help.cmd
Help:
  File "<string>", line 1
    import sys; sys.path.append('D:\\lib'); import ExternalLibrary;lookup = ExternalLibrary.Presentation_control();if ('NUL' == 'NUL'):&    help(lookup.NU
L)&else:&    help(lookup)
                                                                                                                               ^
SyntaxError: invalid syntax

Footnotes

1. I am note sure whether I should pass &, \n, or ; as newline

2. These related answers are not satisfying as they use workarounds.

Community
  • 1
  • 1
noumenal
  • 1,077
  • 2
  • 16
  • 36

1 Answers1

1

Only simple statements can be written on single line separated by semicolons

This was more interesting than it seemed. For the sake of unambiguity you can't write conditional or any other compound statement on semicolon separated single line.

Simple statements

Compound statements

Your options

Rewrite it all to Python

This should not be so hard. Use sys.argv for arguments and

getattr(lookup, "something")

instead of your

lookup.%input%

sys.argv

getattr

Use linefeeds

Write multiline Python script on one cmd line. You can do it using !LF! special.

@echo off
setlocal enableDelayedExpansion
set LF=^


set input=NUL
set /p input="Help: "


python -c "import sys!LF!sys.path.append('D:\\lib')!LF!import ExternalLibrary!LF!lookup = ExternalLibrary.Presentation_control()!LF!if ('%input%' == 'NUL'):!LF!    help(lookup)!LF!else:!LF!    help(lookup.%input%)!LF!"
pacholik
  • 8,607
  • 9
  • 43
  • 55