#Command not giving output

59 messages · Page 1 of 1 (latest)

broken lark
#

the command is giving output just fine. you're not printing it :P

#
use std::process::Command;
fn main() {
    let output = Command::new("cmd")
            .args(["/C", "echo hello"])
            .output()
            .expect("failed to execute process");
    println!("{output:?}");
}
#

ok so std::process::Command uses the builder pattern

#

are you familiar with that concept?

#

what programming experience do you have before rust? will allow me to put it into a more understandable context

#

ah right ok

#

so, in python, the builder pattern isn't really a thing

#

you're more likely to provide all the arguments to a single function

#

however, rust doesn't have varargs

#

so the builder pattern is used instead

#

Command::new("cmd")

#

this makes a new instance of the command struct, with cmd as the command

#

.args(["/C", "echo hello"]) then edits that command struct, setting the args, and returns the edited version

#

.output() means that the command struct will give output

#

and expect is just error handling

#

no, because echo isn't a command

#

or rather

#

it's a command within cmd

#

here let me use an example from my current project

#
Command::new("cmd")
        .args(["/C", "sass", "./scss/index.scss", "./assets/theme.css"])
        .output()
        .expect("Failed to compile SCSS.");
#

sass is a batch script in PATH but I still need to run it with cmd /C

#

tbh I could probably remove the output there

#

the sass file?

#

no

#

I'm running this from %USERPROFILE%/Code/...

#

and sass is located in %USERPROFILE%/ge/sass.bat

#

sass is in my PATH variable

#

kind of

#

PATH on windows is an environment variable which, when you run a command in cmd, is where windows looks for that command

#

eg, cargo is on PATH

#

iirc PATH also has some implications for DLL loading but that might be another variable

#

oh yeah haha

#

so, you have this

#

that builder pattern for command executed the command and returned a new struct, Output

#

Output has three fields

#

status - the exit code effectively

#

stdout and stderr - Vec<u8>

#

yeah in this case that's what you want

#

can you post the error?

#

ah

#

ok so what the stdout function on command does is like Popen in python

#

allows you to set a pipe for stdout

#

what would be better for you here

#

is simply to keep the let output = ... bit be the same

#
use std::process::Command;
fn main() {
    let output = Command::new("cmd")
            .args(["/C", "echo hello"])
            .output()
            .expect("failed to execute process");
    println!("{:?}", output.stdout);
}
#

though you'll probably want to convert output.stdout to a string

#

in which case

#
use std::process::Command;
fn main() {
    let output = Command::new("cmd")
            .args(["/C", "echo hello"])
            .output()
            .expect("failed to execute process");
    println!("{}", String::from_utf8(output.stdout).unwrap());
}
#

output.stdout is a bytestring :P. in python bytes are represented with bytes, but in rust it's just Vec<u8> or some other kind of iterator over a u8 usually

#

the format specifier

slender pecanBOT
broken lark
#

lol

#

not what I wanted

#

this specifies all the formatting

#

np :P sorry I wasn't always clearer

#

uh there might be

#

not sure, never opened one lol

#

right-click on the thread?