Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/allow arguments in commands #22

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions clir/utils/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import platform
import subprocess
from rich import box
from rich import print
from rich.console import Console
from rich.table import Table
from rich.prompt import Prompt
Expand Down Expand Up @@ -98,8 +99,9 @@ def run_command(self):
if current_commands[c]["uid"] == uid:
command = c

if uid:
print(f'Running command: {command}')
command = _replace_arguments(command)
if uid and command:
print(f'[bold green]Running command:[/bold green] {command}')
os.system(command)

def copy_command(self):
Expand Down Expand Up @@ -253,4 +255,30 @@ def _get_commands(tag: str = "", grep: str = ""):

sorted_commands = dict(sorted(current_commands.items(), key=lambda item: item[1]["tag"]))

return sorted_commands
return sorted_commands

def _get_user_input(arg):
return input(f"Enter value for '{arg}': ")

def _replace_arguments(command):
# Use regex to find all arguments with underscores
matches = re.findall(r'_\w+', command)

# Check that all arguments are unique
if len(matches) != len(set(matches)):
print("[bold red]Make sure that all arguments are unique[/bold red]")
return None

# Prompt the user for values for each argument
replacements = {arg: _get_user_input(arg) for arg in matches}

# Split the command into a list
command_list = command.split(" ")

# Replace arguments in the command
for arg, value in replacements.items():
for indx,term in enumerate(command_list):
if arg == term:
command_list[indx] = value

return " ".join(command_list)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "clir"
version = "0.4.3"
version = "0.5.0"
description = "A clear and fast way to store and recover your commands"
authors = ["Elkin Aguas <[email protected]>"]
license = "MIT License"
Expand Down