Coder Social home page Coder Social logo

proc-easier's Introduction

Actions Status

NAME

Proc::Easier - run processes with OO goodness

SYNOPSIS

use Proc::Easier;

# run a command
cmd('ls');

# run command and assign resultant Proc::Easier object to a scalar
my $cmd = cmd('ls');

# get info about the object
say $cmd.out;
say $cmd.err;
say $cmd.exit;    # exitcode

# dump info about the command for debugging
say $cmd;

# run a command, say its output
say cmd('ls').out;

# run a command, say its error
say cmd('ls').err;

# run a command from a different directory
cmd('ls', '/home/dir');

# run a command, die if error encountered, then print info
cmd('a-bad-command', :die);

# make :die the default for all commands until it's turned off
autodie(:on);
cmd('kjkjsdkjf');   # this dies

# turn off :die until it's turned back on
autodie(:off);
cmd('kjkjsdkjf');   # this doesn't die

# create a command with standard OO, don't run it immediately
my $cmd= Proc::Easier.new(cmd => 'ls', :lazy);

# run the command
$cmd.run;

DESCRIPTION

Proc::Easier is a convenient wrapper for the run command using a OO interface to make issuing commands from Raku much easier.

PROC::EASIER Class

Class methods

sub cmd ( Str $cmd, Bool :$dir = '', Bool :$die = False, Bool :$lazy = False )

cmd('ls', '/some/dir', :die);

Convenience method for constructing a Proc::Easier object. Accepts a command, an optional directory command to run the command from, an option to die if an error is encountered, and an option to make the command "lazy." Lazy commands will not be executed unless the run method is called on the object. Otherwise, once the object is constructed, the command will be immediately executed.

multi sub autodie(Bool :$off)

multi sub autodie(Bool :$on)

autodie(:on);
autodie(:off);

Turns on/off the autodie switch. All subsequent Proc::Easier commands will behave as if the :die feature has been turned on or off. Default is off.

Object methods

method new( Str:D :$cmd, Str :$dir = '', Bool :$die = False, Bool :$lazy )

Accepts the same four named variables as the cmd convenience method.

run()

Runs the command.

out()

Returns the string from stdout, if any.

err()

Returns the string from stderr, if any.

exitcode()

exit()

Returns the exit code from the shell, '0' indicates success, other numbers indicate an error.

caller-file()

Returns the name of the file where the call was made.

caller-line()

Returns the line number of the file where the call was made.

Attributes

has Str $.cmd is rw;

Contains the original command.

has Str $.dir is rw;

The directory where the command will be execute.

has Bool $.die is rw;

Whether the command will die if an error is encountered.

AUTHOR

Steve Dondley [email protected]

ACKNOWLEDGEMENTS

Thanks to tbrowder for the Proc::Easy distribution which shamelessly inspired the name of this module and furnished some code and ideas to recycle, too.

COPYRIGHT AND LICENSE

Copyright 2022 Steve Dondley

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

proc-easier's People

Contributors

sdondley avatar tbrowder avatar

Stargazers

 avatar  avatar Mimosinnet avatar Siavash Askari Nasr avatar

Watchers

 avatar

Forkers

melezhik tbrowder

proc-easier's Issues

Using cmd with a Git commit command fails

I tried:

cmd("git commit -a -m'initial commit'");

and got a failure. I tried various ways to escape the commit message but couldn't succeed.

I did have success with the equivalent run command:

run("git", "commit", "-a", '-m"initial comment"');

I'll add more details tommorow.

Thanks

Steve, thanks for the much-improved module, and thanks for the mention!

Questions:

  1. Have you tried redirecting a shell command’s output to files via ‘1>’ as well as ‘2>’?
  2. Have you tried piping the output of a shell command?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.