• How to enter escape character in a positional string argument from the

    From Jach Feng@21:1/5 to All on Sun Dec 4 17:15:12 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Bourne@21:1/5 to Jach Feng on Mon Dec 19 20:48:49 2022
    Jach Feng wrote:
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jach Feng@21:1/5 to All on Mon Dec 19 18:24:49 2022
    Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道:
    Jach Feng wrote:
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Jach Feng on Mon Dec 19 22:36:10 2022
    On 12/19/2022 9:24 PM, Jach Feng wrote:
    Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道:
    Jach Feng wrote:
    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?

    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
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eryk Sun@21:1/5 to Jach Feng on Mon Dec 19 22:35:27 2022
    On 12/19/22, Jach Feng <jfong@ms4.hinet.net> wrote:

    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:

    C:\>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"
    >> " eggs"
    >> and spam"
    ['spam\n', 'eggs\n and spam']

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jach Feng@21:1/5 to All on Tue Dec 20 19:10:46 2022
    Thomas Passin 在 2022年12月20日 星期二上午11:36:41 [UTC+8] 的信中寫道:
    On 12/19/2022 9:24 PM, Jach Feng wrote:
    Mark Bourne 在 2022年12月20日 星期二凌晨4:49:13 [UTC+8] 的信中寫道:
    Jach Feng wrote:
    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?
    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
    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.
    That's what I am taking this path under Windows now, the ultimate solution before Windows has shell similar to bash:-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jach Feng@21:1/5 to All on Tue Dec 20 19:19:05 2022
    ery...@gmail.com 在 2022年12月20日 星期二中午12:35:52 [UTC+8] 的信中寫道:
    On 12/19/22, Jach Feng <jf...@ms4.hinet.net> wrote:

    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"
    " eggs"
    and spam"
    ['spam\n', 'eggs\n and spam']
    Thanks for the information. No idea Windows CMD can take such a trick to enter "\n" :-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Jach Feng on Wed Dec 21 16:01:25 2022
    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.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jach Feng@21:1/5 to All on Tue Dec 20 21:40:13 2022
    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.

    ChrisA
    Really? Where? I can't find it in my Windows 8.1

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Albert-Jan Roskam@21:1/5 to All on Wed Dec 21 13:15:58 2022
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lars Liedtke@21:1/5 to All on Wed Dec 21 13:50:02 2022
    Or you could have "native" bash ($SHELL) with WSL. But I assume not everyone is using it.

    Cheers

    Lars


    Lars Liedtke
    Software Entwickler

    [Tel.] +49 721 98993-
    [Fax] +49 721 98993-
    [E-Mail] lal@solute.de<mailto:lal@solute.de>


    solute GmbH
    Zeppelinstraße 15
    76185 Karlsruhe
    Germany


    [Logo Solute]


    Marken der solute GmbH | brands of solute GmbH
    [Marken]
    [Advertising Partner]

    Geschäftsführer | Managing Director: Dr. Thilo Gans, Bernd Vermaaten
    Webseite | www.solute.de <http://www.solute.de/>
    Sitz | Registered Office: Karlsruhe
    Registergericht | Register Court: Amtsgericht Mannheim
    Registernummer | Register No.: HRB 110579
    USt-ID | VAT ID: DE234663798



    Informationen zum Datenschutz | Information about privacy policy https://www.solute.de/ger/datenschutz/grundsaetze-der-datenverarbeitung.php




    Am 21.12.22 um 13:15 schrieb Albert-Jan Roskam:

    On Dec 21, 2022 06:01, Chris Angelico <rosuav@gmail.com><mailto:rosuav@gmail.com> wrote:

    On Wed, 21 Dec 2022 at 15:28, Jach Feng <jfong@ms4.hinet.net><mailto: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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Lars Liedtke on Wed Dec 21 13:39:14 2022
    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

    . But whenever one feels like writing a "batch file" for cmd,
    "Power Shell" or bash, one could consider to use Python instead.

    Then, of course, there "cmd", ...

    import os
    import cmd

    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

    def precmd(self, line):
    line = line.lower()
    return line

    def parse( arg ):
    return tuple( arg.split() )

    print( __doc__ )
    PythonShell().cmdloop()

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Albert-Jan Roskam on Thu Dec 22 01:29:01 2022
    On Wed, 21 Dec 2022 at 23:16, Albert-Jan Roskam <sjeik_appie@hotmail.com> wrote:



    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?


    Yeah, that's probably the easiest way to get hold of it.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Jach Feng on Thu Dec 22 03:12:50 2022
    On Thu, 22 Dec 2022 at 03:11, Jach Feng <jfong@ms4.hinet.net> wrote:

    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.

    ChrisA
    Really? Where? I can't find it in my Windows 8.1

    Ah, I didn't mean "ships with"; bash for Windows is very much
    available, but you'd have to go fetch it. Try the Windows app store
    first, and if not, a quick web search will find it. Or install Git for
    Windows, which comes with bash.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Stefan Ram on Thu Dec 22 03:19:18 2022
    On Thu, 22 Dec 2022 at 03:11, Stefan Ram <ram@zedat.fu-berlin.de> wrote:

    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.

    Less obvious than you might think - partly because bash is just so
    dang good that it's really really hard to outdo it :) Sure, bash has a
    lot of weird and wonky edge cases, but it's an incredibly practical
    shell to use.

    When commands are typed manually, this might be a bit verbose,
    though. I mean

    os.chdir( r'C:\EXAMPLE' )

    versus

    CD C:\EXAMPLE

    Exactly. What's good for a programming language is often not good for a shell.

    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

    Sure, you can always create your own shell. But I think you'll find
    that, as you start expanding on this, you'll end up leaning more
    towards "implementing bash-like and/or cmd-like semantics in Python"
    rather than "creating a Python shell". Shells, in general, try to
    execute programs as easily and conveniently as possible. Programming
    languages try to stay inside themselves and do things, with subprocess
    spawning being a much less important task.

    Fun challenge: see how much you can do in bash without ever forking to
    another program. And by "fun", I mean extremely difficult, and by
    "challenge" I really mean "something you might have to do when your
    system is utterly hosed and all you have available is one root shell".

    It's amazing how far you can go when your hard drive has crashed and
    you desperately need to get one crucial login key that you thought you
    had saved elsewhere but hadn't.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dennis Lee Bieber@21:1/5 to All on Wed Dec 21 12:21:35 2022
    On 21 Dec 2022 13:39:14 GMT, ram@zedat.fu-berlin.de (Stefan Ram) declaimed
    the following:

    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' )

    os.chdir("C:/Example")

    is perfectly valid in the Windows API. It is only the command line
    processor that requires \ to differentiate from command options.

    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...

    "CD C:\Example"

    (with the quotes) is sufficient. (Hmm I may have to check that -- it may
    only CD for the one command, then revert to parent directory)


    --
    Wulfraed Dennis Lee Bieber AF6VN
    wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Dennis Lee Bieber on Wed Dec 21 17:49:35 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dennis Lee Bieber@21:1/5 to All on Wed Dec 21 21:18:07 2022
    On 21 Dec 2022 17:49:35 GMT, ram@zedat.fu-berlin.de (Stefan Ram) declaimed
    the following:

    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.

    Probably -- AREXX was the most capable version of REXX I've encountered (Regina REXX and ooREXX seem to be limited in the command processors it understands -- basically the equivalent of direct execution ["exec"? style]
    or with a shell). It allowed /any/ application that opened an AREXX port to
    be command processor. Even the basic text editor could be controlled via
    AREXX. Heck, one could even write AREXX scripts that could open ports and become command processors for other scripts.


    --
    Wulfraed Dennis Lee Bieber AF6VN
    wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)