Code Monkey home page Code Monkey logo

cmd's Introduction

cmd

A C# Library to run external programs / commands in a simpler way. It is inspired from the sh library for Python, and is intended to showcase the "dynamic" features of C#

How to get it?

Cmd is available through the Nuget Package Manager.

Or, you can build it from source.

How to use it?

Create a dynamic instance of Cmd:

dynamic cmd = new Cmd();

Now, you can call commands off cmd:

cmd.git.clone("http://github.com/manojlds/cmd");

The above would be equivalent to git clone http://github.com/manojlds/cmd.

You can pass flags by naming the arguments:

cmd.git.log(grep: "test");

The above would be equivalent to git log --grep test

Or:

cmd.git.branch(a: true);

which would be equivalent to git branch -a

Note that single character flags are mapped as -<flag> and multi-character ones are mapped as --<flag>

Also, non-string values are ignored and if there is no flag, the argument is not considered.

You can call multiple commands off the same instance of cmd:

var gitOutput = cmd.git();
var svnOutput = cmd.svn();

Note that the commands can be case sensitive, and as such cmd.git is not same as, say, cmd.Git.

How to set environment variables?

Environment variables can be set for the process by calling ._Env method on an instance of Cmd and pass the set of environment variables with their values as a Dictionary<string, string>:

cmd._Env(new Dictionary<string, string> { { "GIT_DIR", @"C:\" } });

Note that this replaces existing variables with the new values.

Shells

You can use cmd to run command on, well, cmd and Powershell. Choose the shell you want to use while creating cmd:

dynamic cmd = new Cmd(Shell.Cmd);
dynamic posh = new Cmd(Shell.Powershell);
cmd.dir();

cmd.dir() is equivalent to cmd /c dir

When using Shell.Cmd, flags are constructed using / instead of - and --

Powershell support is still a work in progress.

What's ahead?

cmd is in a very nascent stage. More sh like goodness coming soon.

cmd's People

Contributors

josteink avatar manojlds avatar sriv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cmd's Issues

How to hide cmd windows?

When I run command, the main cmd opened and close fastly.
How to hide it?

dynamic cmd = new Cmd();
var output = cmd.ipconfig();
MessageBox.Show(output);

Environment variable support

This is a fantastic project. I'm thinking of using it in a project to convert hundreds of 4NT (a command line syntax that is close enough to regular Windows CMD) scripts to a C# project that uses your library.

Here are a few features that would make things easier:

  • A call to Environment.ExpandEnvironmentVariables within ArgumentBuilder/CmdArgumentBuilder, which would allow for calls like cmd.sampleProgram( param1: "%APPDATA%\Foo" ) --> sampleProgram --param1 %APPDATA%\Foo
  • A call that would allow environment variables to be set within the current process

Question

What happens if someone wants to use one cmd instance twice, like
Cmd.dir(s:true)();
Cmd.type("my file.txt")();

I would recommend to return new object instead of this, and make it a combination of the current object and arguments. This will also allow partial specification objects, like

Var gitpull = cmd.git.pull;
gitpull("./")();

Code style

I think python style isn't acceptable, code should be in C# style.

var cmd = new cmd();
cmd.FileName = "git";

var option = new option ();
option.name = "grep";
cmd.AddOption(option );

var argument = new argument();
argument.name = "grep";
argument.parameter = "test";
cmd.AddArgument(argument);

cmd.Execute;
var response = cmd.GetResponse;

Return StandardError stream

Sometimes it is useful to have the error stream instead of stdout (esp. if the errorlevel != 0)

For example:

var x = cmd.git.describe(); //could return empty string, though console would show 'fatal: No names found, cannot describe anything.'

Not sure of a good interface for this ... perhaps something like this

var x = cmd.git.describe();

//where x is an object which has the following properties?
x.ErrorLevel : int
x.StdOut : string
x.StdErr : string

64-bit

dynamic cmd = new Cmd();
cmd.dir()();

produces a Win32Exception "The system cannot find the file specified"

I'm running 64-bit Windows 8, thinking it has to do with the File System Redirector

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.