I have a script using the argparse module. I want to enter the string "step\x0A" as one of its positional arguments. I expect this string has a length of 5, but it gives 8. Obviously the escape character didn't function correctly. How to do it?
Jach Feng wrote:That's really good for Linux user! How about Windows?
I have a script using the argparse module. I want to enter the string "step\x0A" as one of its positional arguments. I expect this string has a length of 5, but it gives 8. Obviously the escape character didn't function correctly. How to do it?That depends on the command-line shell you're calling your script from.
In bash, you can include a newline in a quoted string:
./your_script 'step
'
(the closing quote is on the next line)
Or if you want to do it on a single line (or use other escape
sequences), you can use e.g.:
./your_script $'step\x0a'
(dollar sign before a single-quoted string which contains escape sequences)
--
Mark.
Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道:
Jach Feng wrote:That's really good for Linux user! How about Windows?
I have a script using the argparse module. I want to enter the string "step\x0A" as one of its positional arguments. I expect this string has a length of 5, but it gives 8. Obviously the escape character didn't function correctly. How to do it?That depends on the command-line shell you're calling your script from.
In bash, you can include a newline in a quoted string:
./your_script 'step
'
(the closing quote is on the next line)
Or if you want to do it on a single line (or use other escape
sequences), you can use e.g.:
./your_script $'step\x0a'
(dollar sign before a single-quoted string which contains escape sequences) >>
--
Mark.
That's really good for Linux user! How about Windows?
On 12/19/2022 9:24 PM, Jach Feng wrote:That's what I am taking this path under Windows now, the ultimate solution before Windows has shell similar to bash:-)
Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道:One way is to process the argument after it gets into Python rather than before. How hard that will be depends on how general you need the
Jach Feng wrote:That's really good for Linux user! How about Windows?
I have a script using the argparse module. I want to enter the string "step\x0A" as one of its positional arguments. I expect this string has a length of 5, but it gives 8. Obviously the escape character didn't function correctly. How to do it?That depends on the command-line shell you're calling your script from. >>
In bash, you can include a newline in a quoted string:
./your_script 'step
'
(the closing quote is on the next line)
Or if you want to do it on a single line (or use other escape
sequences), you can use e.g.:
./your_script $'step\x0a'
(dollar sign before a single-quoted string which contains escape sequences)
--
Mark.
argument to be. For your actual example, the argument comes into Python
as if it were
arg1 = r"step\x0A" # or "step\\x0a"
You can see if there is an "\\x":
pos = arg1.find('\\x') # 4
Replace or use a regex to replace it:
arg1_fixed = arg1.replace('\\x0A', '\n')
Naturally, if "\\x0A" is only a special case and other combinations are possible, you will need to figure out what you need and do some more complicated processing.
On 12/19/22, Jach Feng <jf...@ms4.hinet.net> wrote:Thanks for the information. No idea Windows CMD can take such a trick to enter "\n" :-)
That's really good for Linux user! How about Windows?In CMD, typing the "^" escape character at the end of a line ignores
the newline and prompts for "more" input. If you press enter again,
you'll get another "more" prompt in which you can write the rest of
the command line. Command-line arguments are separated by spaces, so
you have to start the next line with a space if you want it to be a
new argument. Also, "^" is a literal character when it's in a
double-quoted string, which requires careful use of quotes. For
example:
py -c "import sys; print(sys.orig_argv[3:])" spam^More?
More? eggs^
More?
More? " and spam"
['spam\n', 'eggs\n and spam']
The above is easier in PowerShell, which supports entering multiline
strings without having to escape the newline. The second-level prompt
is ">> ". For example:
py -c "import sys; print(sys.orig_argv[3:])" spam"['spam\n', 'eggs\n and spam']
" eggs"
and spam"
That's what I am taking this path under Windows now, the ultimate solution before Windows has shell similar to bash:-)
On Wed, 21 Dec 2022 at 15:28, Jach Feng <jf...@ms4.hinet.net> wrote:Really? Where? I can't find it in my Windows 8.1
That's what I am taking this path under Windows now, the ultimate solution before Windows has shell similar to bash:-)Technically, Windows DOES have a shell similar to bash. It's called
bash. :) The trouble is, most people use cmd.exe instead.
ChrisA
Or you could have "native" bash ($SHELL) with WSL.
On Dec 21, 2022 06:01, Chris Angelico <rosuav@gmail.com> wrote:
On Wed, 21 Dec 2022 at 15:28, Jach Feng <jfong@ms4.hinet.net> wrote:
That's what I am taking this path under Windows now, the ultimate solution before Windows has shell similar to bash:-)
Technically, Windows DOES have a shell similar to bash. It's called
bash. :) The trouble is, most people use cmd.exe instead.
=====
I use Git Bash quite a lot: https://gitforwindows.org/
Is that the one you're referring to?
Chris Angelico 在 2022年12月21日 星期三下午1:02:01 [UTC+8] 的信中寫道:
On Wed, 21 Dec 2022 at 15:28, Jach Feng <jf...@ms4.hinet.net> wrote:
That's what I am taking this path under Windows now, the ultimate solution before Windows has shell similar to bash:-)Technically, Windows DOES have a shell similar to bash. It's called
bash. :) The trouble is, most people use cmd.exe instead.
ChrisAReally? Where? I can't find it in my Windows 8.1
Lars Liedtke <lal@solute.de> writes:
Or you could have "native" bash ($SHELL) with WSL.
In this newsgroup, it would actually be obvious to use Python.
When commands are typed manually, this might be a bit verbose,
though. I mean
os.chdir( r'C:\EXAMPLE' )
versus
CD C:\EXAMPLE
class PythonShell( cmd.Cmd ):
intro = 'Welcome to the Python shell. Type help or ? to list commands.\n'
prompt = '(Python) '
file = None
def do_cd( self, arg ):
'change directory: CD C:\EXAMPLE'
os.chdir( *parse( arg ))
def do_bye( self, arg ):
'Exit: BYE'
print( 'Thank you for using the Python Shell!' )
return True
Lars Liedtke <lal@solute.de> writes:
Or you could have "native" bash ($SHELL) with WSL.
In this newsgroup, it would actually be obvious to use Python.
When commands are typed manually, this might be a bit verbose,
though. I mean
os.chdir( r'C:\EXAMPLE' )
If that is still too much to enter, I'd recommend one look at REXX...
Any statement that is not parsable as a REXX command is normally sent to
the shell (unless one has specified a different command processor), so...
Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:
If that is still too much to enter, I'd recommend one look at REXX... >>Any statement that is not parsable as a REXX command is normally sent to >>the shell (unless one has specified a different command processor), so...
I still have vague but fond memories of embedding something
into my texts in the Amiga Cygnus-Ed editor which then would
serve as Cygnus-Ed "macros" activated via hotkeys. I believe
these snippets were written by me in AREXX (REXX for the Amiga,
1987 by William S. Hawes). I forgot all details, though.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 79:40:46 |
Calls: | 6,716 |
Files: | 12,247 |
Messages: | 5,357,918 |