Python click to restart a command with all parameters cleared - python-click

My script below is saved in 'bike.py' and registered as bike shell command. I'd like to run a simple bike filter -c chicago -m 1 -d 1 command to explore data for chicago as city, 1 as month and 1 as day of week. I use prompt=True to catch any options unspecified in the command.
I'd also like to be able to restart the command at the end and have all the existing parameters cleared, so that the command will prompt me for city, month and day of week at restart. However, the code can't do that. It just runs what's inside the script and gives me error because the script can't run without the arguments passed in.
How should I do this with click?
#click.group()
#click.pass_context
def main():
ctx.ensure_object(dict)
#main.command()
#click.option('-city', '-c', prompt=True)
#click.option('-month', '-m', prompt=True)
#click.option('-day_of_week', '-d', prompt=True)
#click.pass_context
def filter(ctx, city, month, day_of_week):
# load filtered data
...
# restart the program
if click.confirm('Restart to explore another dataset?') if not False:
import sys
sys.argv.clear()
ctx.invoke(filter)

You could change your filter command so you do the prompting manually. I don't think Click will know to prompt otherwise, since this is not a common use-case and doesn't conform to the no-magic click style guide.
#main.command()
#click.option('-city', '-c')
#click.option('-month', '-m')
#click.option('-day_of_week', '-d')
#click.pass_context
def filter(ctx, city, month, day_of_week):
# prompt if not passed in
if city is None:
city = click.prompt('City: ')
if month is None:
month = click.prompt('Month: ')
if day_of_week is None:
day_of_week = click.prompt('Day of the week: ')
# load filtered data
...
# restart the program
if click.confirm('Restart to explore another dataset?'):
ctx.invoke(filter)
Alternatively, if you really want to rely on the built-in functionality, you might be able to get some mileage by overriding the Option class and prompt functionality.

Related

Weechat add hook command in every send Messages

I am a new user in Weechat. I have made a customized weechat script from another existing script. To run that script I need to use hook command every time i send text to channel . For example /myhook message_to_channel . I want to automate this process so every time I send messages to a channel i wouldn't need to write /myhook but just message_to_channel . Is there any way I could make it happen. Thanks.
The alias plugin that is included in WeeChat by default can facilitate this.
You can run the following command to create the alias you want.
/alias add message_to_channel /myhook message_to_channel
After that, you should be able to just use /message_to_channel. You can also add arguments if you like:
$n: argument 'n' (between 1 and 9)
$-m: arguments from 1 to 'm'
$n-: arguments from 'n' to last
$n-m: arguments from 'n' to 'm'
$*: all arguments
$~: last argument
$var: where "var" is a local variable of buffer (see /buffer localvar)
examples: $nick, $channel, $server, $plugin, $name
The documentation is here.

Powershell text editing

I've got a .bch file that i usually manually edit whenever I need to restore a database. The changes involve removing # from the start of specific lines and changing the database name of the lines where the # is removed to a new name that will be unique every time the script is run.
#DATABASE "YYYYY"
MOVE "YYYYYY"
#TO "H:\MSSQL.1\Data\YYYYY.mdf"
Change to
DATABASE "XXXXX"
MOVE "YYYYYY"
TO "H:\MSSQL.1\Data\XXXXX.mdf"
Would this be possible to do via a script in powershell?
Adding to question as I was pretty vague:
Hi Team, Sorry for the vague question I will get better as I ask more I'm sure. I'm aware of the replace function but in my research I couldn't really find anything to find specific characters. A function involving something like:
"On line Where character 1 from the left = # replace YYYY with XXX"
Then Step two of the shell script could be:
"on line Where string "XXXX" exists delete character 1 from the left"
I just can't find on google if functions like this exist
You haven't given much to go on, but have a start with:
cat somefile.bch | %{$_ -replace "expression","replace"} > newfile.bch

Manage inputs from external command in a powershell script

First, I would like to apologize in case that the title is not descriptive enough, I'm having a hard time dealing with this problem. I'm trying to build an automation for a svn merge using a powershell script that will be executed for another process. The function that I'm using looks like this:
function($target){
svn merge $target
}
Now, my problem occurs when there are conflicts in the merge. The default behavior of the command is request an input from the user and proceed accordingly. I would like to automatize this process using predefined values (show the differences and then postpone the merge), but I haven't found a way to do it. In summary, the workflow that I am looking to accomplish is the following:
Detect whether the command execution requires any input to proceed
Provide a default inputs (in my particular case "df" and then "p")
Is there any way to do this in powershell? Thank you so much in advance for any help/clue that you can provide me.
Edit:
To clarify my question: I would like to automatically provide a value when a command executed within a powershell script require it, like in the following example:
Requesting user input
Edit 2:
Here is a test using the snippet provided by #mklement0. Unfortunately, It didn't work as expected, but I thought it was wort to add this edition to clarify the question per complete
Expected behavior:
Actual result:
Note:
This answer does not solve the OP's problem, because the specific target utility, svn, apparently suppresses prompts when the process' stdin input isn't coming from a terminal (console).
For utilities that do still prompt, however, the solution below should work, within the constraints stated.
Generally, before attempting to simulate user input, it's worth investigating whether the target utility offers programmatic control over the behavior, via its command-line options, which is both simpler and more robust.
While it would be far from trivial to detect whether a given external command is prompting for user input:
you can blindly send the presumptive responses,
which assumes that no situational variations are needed (except if a particular calls happens not to prompt at all, in which case the input is ignored).
Let's assume the following batch file, foo.cmd, which puts up 2 prompts and echoes the input:
#echo off
echo begin
set /p "input1=prompt 1: "
echo [%input1%]
set /p "input2=prompt 2: "
echo [%input2%]
echo end
Now let's send responses one and two to that batch file:
C: PS> Set-Content tmp.txt -Value 'one', 'two'; ./foo.cmd '<' tmp.txt; Remove-Item tmp.txt
begin
prompt 1: one
[one]
prompt 2: two
[two]
end
Note:
For reasons unknown to me, the use of an intermediate file is necessary for this approach to work on Windows - 'one', 'two' | ./foo.cmd does not work.
Note how the < must be represented as '<' to ensure that it is passed through to cmd.exe and not interpreted by PowerShell up front (where < isn't supported).
By contrast, 'one', 'two' | ./foo does work on Unix platforms (PowerShell Core).
You can store the SVN command line output into a variable and parse through that and branch as you desire. Each line of output is stored into a new enumerator (cli output stored in PS variables is in array format)
$var = & svn merge $target
$var

same input for interactive command in Perl script

In Perl script I am using command of unit(external command) using system
but that command need some run time input from user. Every time I want to give similar input which is stored in variable.
My command is:
system("dt ci $dest_file");
after this command my script is waiting for user input,
lets say I want to give "re base" as input to this every time
How can I give fix input every time it asks?
Please help, I am working on windows 7.
I want to make script completely automated... it should not wait for user input
If you call your script like this:
perl test.pl < input.txt
It should take the content of the file as an input. ( this is working with STDIN read )

Can't make a directory using a variables value - Command Prompt

I'm trying to set up a simple backup process for a folder on my C drive that will back that folder up to another location on the network. I know how to create a scheduled task but I'm struggling to understand why my command prompt code won't work - I'm a novice when it comes to the Command Prompt though!
So my question is two fold:
Why does echo %variableName% not return the variable value - it only returns %variableName%.
This is what I type in:
#echo off
set varA = 5
echo %varA%
%varA% <- This is what its popping out
Do I need different preceeding and succeeding characters for this?
I want to create a folder with the date for the name (I do realize that are quite a few questions out there on this but they didn't work), how do I do it?
Here is what I tried:
set folder_name = %DATE:/=_%
set folder_name <- Display value for folder_name
folder_name = Wed 11_06_2013 <- Actual value
When I try to do this:
mkdir %folder_name%
dir
Creates a folder with this %_date% as the name.
Where am I going wrong?
Thanks
Space is significant in SET statements.
SET varspace=spacevalue
will set a variable named "varspace" to "spacevalue"
Remove the spaces and it should be plain sailing...
Oh - except that if the variable contains a space, then commands such as MD or mkdir (which are synonyms) require "rabbits ears" around the value, thus:
mkdir "%folder_name%"