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}
python bash
New contributor
add a comment |
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}
python bash
New contributor
1
Just read the standard input?
– Reut Sharabani
9 hours ago
1
You can use nice editing with readline; but parsing things likefile{1..7}
, you'd have to implement yourself.
– Amadan
9 hours ago
add a comment |
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}
python bash
New contributor
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
python bash
New contributor
New contributor
New contributor
asked 9 hours ago
Greg Splitt
1
1
New contributor
New contributor
1
Just read the standard input?
– Reut Sharabani
9 hours ago
1
You can use nice editing with readline; but parsing things likefile{1..7}
, you'd have to implement yourself.
– Amadan
9 hours ago
add a comment |
1
Just read the standard input?
– Reut Sharabani
9 hours ago
1
You can use nice editing with readline; but parsing things likefile{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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 9 hours ago
AracKnight
387
387
add a comment |
add a comment |
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.
Greg Splitt is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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