ngs-lang / ngs

Next Generation Shell (NGS)
https://ngs-lang.org/
GNU General Public License v3.0
1.4k stars 43 forks source link

[Question] How do you make a script that takes command line arguments? #557

Closed MinmoTech closed 2 years ago

MinmoTech commented 2 years ago

I'm making a script that reroutes audio with pipewire on my PC. For this I have to pass the name of an appliation.

Currently I'm doing this with echo "ProgramName" | my_script.ngs but ideally I'd be able to use this by doing my_script.ngs "ProgramName". I could also do this by making a wrapper script in another scripting language, but that also doesn't feel right.

Thanks for any help!

My script in question:

#!/usr/bin/env ngs
if not(Bool(ExitCode(`pactl list short sinks | grep combine`))) {
    $(pactl load-module module-null-sink media.class=Audio/Sink sink_name=combine channel_map=stereo)
}
program_name = read() - Sfx("\n")
outputs = (`pw-link --output | grep -i ${program_name}` - Sfx("\n")).split("\n")
inputs = (`pw-link --input | grep combine` - Sfx("\n")).split("\n")
pw-link ${outputs[0]} ${inputs[0]}
pw-link ${outputs[1]} ${inputs[1]}
ilyash-b commented 2 years ago

Alternative 1

Alternative 2

Use command line arguments available in ARGV: program_name = read() - Sfx("\n") becomes program_name = ARGV[0].


Let us know how it goes

ilyash-b commented 2 years ago

Unrelated improvement suggestion: .split("\n") -> .lines() and (EXPR - Sfx("\n")).split("\n") also -> EXPR.lines()

ilyash-b commented 2 years ago

If I understand correctly,

if not(Bool(ExitCode(`pactl list short sinks | grep combine`)))

can become

if not($(pactl list short sinks | grep combine))

Edit: alternatively:

if not(`pactl list short sinks`.has('combine'))
MinmoTech commented 2 years ago

Thanks a lot, this is perfect! And with your tips my script looks a lot more readable now:

#!/usr/bin/env ngs
if not(`pactl list short sinks`.has('combine')){
    $(pactl load-module module-null-sink media.class=Audio/Sink sink_name=combine channel_map=stereo)
}
program_name = ARGV[0]
outputs = `pw-link --output | grep -i ${program_name}`.lines()
inputs = `pw-link --input | grep combine`.lines()
pw-link ${outputs[0]} ${inputs[0]}
pw-link ${outputs[1]} ${inputs[1]}

And thank you for this project, I like it a lot since I found it thanks to your console interview!