How to Prefill Command Line Input

How to prefill command line input

You need to use the TIOCSTI ioctl. Here's an example C program that shows how it works:

#include <sys/ioctl.h>

main()
{
char buf[] = "date";
int i;
for (i = 0; i < sizeof buf - 1; i++)
ioctl(0, TIOCSTI, &buf[i]);
return 0;
}

Compile this and run it and "date" will be buffered as input on stdin, which your shell will read after the program exits. You can roll this up into a command that lets you stuff anything into the input stream and use that command in your bash script.

Is it possible to prefill a input() in Python 3's Command Line Interface?

If your Python interpreter is linked against GNU readline, input() will use it. In this case, the following should work:

import readline

def input_with_prefill(prompt, text):
def hook():
readline.insert_text(text)
readline.redisplay()
readline.set_pre_input_hook(hook)
result = input(prompt)
readline.set_pre_input_hook()
return result

How to prefill the command prompt with some chars?

You need to get the batch script inside the conditional compilation block:

bar.bat

@if (@CodeSection == @Batch) @then

@echo off
if NOT "%1"=="" goto PROGRAM
cmd /k bar.bat DONT_CLOSE_PROMPT
EXIT

:PROGRAM
set SendKeys=CScript //nologo //E:JScript "%~F0"
SET PATH=%PATH%;D:\Programme\Git\bin
d:
cd "D:\username\Documents\Visual Studio 2013\Projects"
REM cls
%SendKeys% "git clone "

goto :EOF

@end

// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

Pre-filling a prompt in Bash

You can wrap the command in rlwrap, which provides instant readline capabilities: https://github.com/hanslub42/rlwrap

(rlwrap -P does what you want)

As far as a pure bash solution is concerned for the 3.2 line (which i am presuming you are using), I dont think its possible

Pre-fill stdin for a process in bash

You can pipe multiple commands in sequence, where the the first writes a header and the second is just cat to relay input from stdin to stdout:

{ echo "$HEADER"; cat; } | yourcommand

The problem with your approach is that you can only ever have one stdin, while you try to use two. echo "foo" | cat /dev/stdin /dev/tty could have been an option, but this unnecessarily relies on having a terminal.

Is there anyway to have preset data for user input in a batch file?

The method below have the inconvenience that the screen must be cleared before the user enter the data, but I am working trying to solve this point:

EDIT: I fixed the detail of the first version

@if (@CodeSection == @Batch) @then

@echo off
rem Enter the prefill value
CScript //nologo //E:JScript "%~F0" "First Last"
rem Read the variable
echo -----------------------------------------------------------
set /P "Author=Please enter your name: "
echo Author=%Author%
goto :EOF

@end

WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));

For further details, see this post.

Bash: Set up a new command on a new line without executing

How about

read -e -p"$PWD> " -i"ls -al " cmd; eval "$cmd"

Does Python Click prompt have any prefill options?

Prompts are not the most user-friendly for editing text.

Fortunately, there is a way around this.

Take, for example, git commit.

If you don't specify the --message or -m option, git commit opens up an editor for the user to type a commit message in.

You might want to consider prompting your users with a text-editor too.

You can achieve this with python-click as follows:

import click

def promp_user_with_editor(old_param: str) -> str:
comment = "# Edit Domain Name:\n"
editor_content = f"{comment}{old_param}"
result = click.edit(editor_content)
# In real code, you would properly handle the user's input here.
new_param = result.split(comment)[1].strip()
return new_param

@click.command()
def main():
new_param = promp_user_with_editor(old_param="testdomain.com")
click.echo(f"{new_param=}")

if __name__ == "__main__":
main()

Now running this code as:

python3 the_name_of_the_above_file.py

Will prompt me with:

# Edit Domain Name:
testdomain.com

If I change that to:

# Edit Domain Name:
stackoverflow.com

The output is:

new_param='stackoverflow.com'


Related Topics



Leave a reply



Submit