How to have a python input that behaves like bash input?











up vote
-2
down vote

favorite












I want to get user input in a python script after the script has begun, not as a command line argument.



I tried using pythons input method, but this behaves like a standard text-box. Is there an input type in python that behaves like the bash terminal with predictive suggestions and bash syntax such as file{1..7}










share|improve this question







New contributor




Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Just read the standard input?
    – Reut Sharabani
    9 hours ago






  • 1




    You can use nice editing with readline; but parsing things like file{1..7}, you'd have to implement yourself.
    – Amadan
    9 hours ago

















up vote
-2
down vote

favorite












I want to get user input in a python script after the script has begun, not as a command line argument.



I tried using pythons input method, but this behaves like a standard text-box. Is there an input type in python that behaves like the bash terminal with predictive suggestions and bash syntax such as file{1..7}










share|improve this question







New contributor




Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    Just read the standard input?
    – Reut Sharabani
    9 hours ago






  • 1




    You can use nice editing with readline; but parsing things like file{1..7}, you'd have to implement yourself.
    – Amadan
    9 hours ago















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I want to get user input in a python script after the script has begun, not as a command line argument.



I tried using pythons input method, but this behaves like a standard text-box. Is there an input type in python that behaves like the bash terminal with predictive suggestions and bash syntax such as file{1..7}










share|improve this question







New contributor




Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I want to get user input in a python script after the script has begun, not as a command line argument.



I tried using pythons input method, but this behaves like a standard text-box. Is there an input type in python that behaves like the bash terminal with predictive suggestions and bash syntax such as file{1..7}







python bash






share|improve this question







New contributor




Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 9 hours ago









Greg Splitt

1




1




New contributor




Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Greg Splitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    Just read the standard input?
    – Reut Sharabani
    9 hours ago






  • 1




    You can use nice editing with readline; but parsing things like file{1..7}, you'd have to implement yourself.
    – Amadan
    9 hours ago
















  • 1




    Just read the standard input?
    – Reut Sharabani
    9 hours ago






  • 1




    You can use nice editing with readline; but parsing things like file{1..7}, you'd have to implement yourself.
    – Amadan
    9 hours ago










1




1




Just read the standard input?
– Reut Sharabani
9 hours ago




Just read the standard input?
– Reut Sharabani
9 hours ago




1




1




You can use nice editing with readline; but parsing things like file{1..7}, you'd have to implement yourself.
– Amadan
9 hours ago






You can use nice editing with readline; but parsing things like file{1..7}, you'd have to implement yourself.
– Amadan
9 hours ago














1 Answer
1






active

oldest

votes

















up vote
0
down vote













It would be helpful if you'd add which operating system you are using and for what purpose you want the 'bash-like' input. The most common way I guess would be to implement a CLI yourself and then calling the bash, but this may be a bit over the top depending on what you want to achieve.



For implementing a CLI I suggest to have a look at the click-package. Just a quick example:



import click
import click_repl
from subprocess import Popen, DEVNULL
import os
from prompt_toolkit.history import FileHistory

@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx):
if ctx.invoked_subcommand is None:
ctx.invoke(repl)

@cli.command()
@click.option("--directory", "-d", prompt=True, help="Directory to cd into")
def cd(directory):
command = "cd {0}".format(directory)
Popen(command, shell=True, stdin=None, stdout=DEVNULL, stderr=None)

@cli.command()
def repl():
prompt_kwargs = {
'history': FileHistory(os.path.expanduser('~/.repl_history'))
}
click_repl.repl(click.get_current_context(), prompt_kwargs)

try:
cli(obj={})
except SystemExit:
pass


It is important to catch a SystemExit because click always throw one after executing a command. And you have to implement every bash command you want to allow as a click command.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Greg Splitt is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203021%2fhow-to-have-a-python-input-that-behaves-like-bash-input%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    It would be helpful if you'd add which operating system you are using and for what purpose you want the 'bash-like' input. The most common way I guess would be to implement a CLI yourself and then calling the bash, but this may be a bit over the top depending on what you want to achieve.



    For implementing a CLI I suggest to have a look at the click-package. Just a quick example:



    import click
    import click_repl
    from subprocess import Popen, DEVNULL
    import os
    from prompt_toolkit.history import FileHistory

    @click.group(invoke_without_command=True)
    @click.pass_context
    def cli(ctx):
    if ctx.invoked_subcommand is None:
    ctx.invoke(repl)

    @cli.command()
    @click.option("--directory", "-d", prompt=True, help="Directory to cd into")
    def cd(directory):
    command = "cd {0}".format(directory)
    Popen(command, shell=True, stdin=None, stdout=DEVNULL, stderr=None)

    @cli.command()
    def repl():
    prompt_kwargs = {
    'history': FileHistory(os.path.expanduser('~/.repl_history'))
    }
    click_repl.repl(click.get_current_context(), prompt_kwargs)

    try:
    cli(obj={})
    except SystemExit:
    pass


    It is important to catch a SystemExit because click always throw one after executing a command. And you have to implement every bash command you want to allow as a click command.






    share|improve this answer

























      up vote
      0
      down vote













      It would be helpful if you'd add which operating system you are using and for what purpose you want the 'bash-like' input. The most common way I guess would be to implement a CLI yourself and then calling the bash, but this may be a bit over the top depending on what you want to achieve.



      For implementing a CLI I suggest to have a look at the click-package. Just a quick example:



      import click
      import click_repl
      from subprocess import Popen, DEVNULL
      import os
      from prompt_toolkit.history import FileHistory

      @click.group(invoke_without_command=True)
      @click.pass_context
      def cli(ctx):
      if ctx.invoked_subcommand is None:
      ctx.invoke(repl)

      @cli.command()
      @click.option("--directory", "-d", prompt=True, help="Directory to cd into")
      def cd(directory):
      command = "cd {0}".format(directory)
      Popen(command, shell=True, stdin=None, stdout=DEVNULL, stderr=None)

      @cli.command()
      def repl():
      prompt_kwargs = {
      'history': FileHistory(os.path.expanduser('~/.repl_history'))
      }
      click_repl.repl(click.get_current_context(), prompt_kwargs)

      try:
      cli(obj={})
      except SystemExit:
      pass


      It is important to catch a SystemExit because click always throw one after executing a command. And you have to implement every bash command you want to allow as a click command.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        It would be helpful if you'd add which operating system you are using and for what purpose you want the 'bash-like' input. The most common way I guess would be to implement a CLI yourself and then calling the bash, but this may be a bit over the top depending on what you want to achieve.



        For implementing a CLI I suggest to have a look at the click-package. Just a quick example:



        import click
        import click_repl
        from subprocess import Popen, DEVNULL
        import os
        from prompt_toolkit.history import FileHistory

        @click.group(invoke_without_command=True)
        @click.pass_context
        def cli(ctx):
        if ctx.invoked_subcommand is None:
        ctx.invoke(repl)

        @cli.command()
        @click.option("--directory", "-d", prompt=True, help="Directory to cd into")
        def cd(directory):
        command = "cd {0}".format(directory)
        Popen(command, shell=True, stdin=None, stdout=DEVNULL, stderr=None)

        @cli.command()
        def repl():
        prompt_kwargs = {
        'history': FileHistory(os.path.expanduser('~/.repl_history'))
        }
        click_repl.repl(click.get_current_context(), prompt_kwargs)

        try:
        cli(obj={})
        except SystemExit:
        pass


        It is important to catch a SystemExit because click always throw one after executing a command. And you have to implement every bash command you want to allow as a click command.






        share|improve this answer












        It would be helpful if you'd add which operating system you are using and for what purpose you want the 'bash-like' input. The most common way I guess would be to implement a CLI yourself and then calling the bash, but this may be a bit over the top depending on what you want to achieve.



        For implementing a CLI I suggest to have a look at the click-package. Just a quick example:



        import click
        import click_repl
        from subprocess import Popen, DEVNULL
        import os
        from prompt_toolkit.history import FileHistory

        @click.group(invoke_without_command=True)
        @click.pass_context
        def cli(ctx):
        if ctx.invoked_subcommand is None:
        ctx.invoke(repl)

        @cli.command()
        @click.option("--directory", "-d", prompt=True, help="Directory to cd into")
        def cd(directory):
        command = "cd {0}".format(directory)
        Popen(command, shell=True, stdin=None, stdout=DEVNULL, stderr=None)

        @cli.command()
        def repl():
        prompt_kwargs = {
        'history': FileHistory(os.path.expanduser('~/.repl_history'))
        }
        click_repl.repl(click.get_current_context(), prompt_kwargs)

        try:
        cli(obj={})
        except SystemExit:
        pass


        It is important to catch a SystemExit because click always throw one after executing a command. And you have to implement every bash command you want to allow as a click command.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 9 hours ago









        AracKnight

        387




        387






















            Greg Splitt is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Greg Splitt is a new contributor. Be nice, and check out our Code of Conduct.













            Greg Splitt is a new contributor. Be nice, and check out our Code of Conduct.












            Greg Splitt is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203021%2fhow-to-have-a-python-input-that-behaves-like-bash-input%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check