Click here to Skip to main content
15,885,546 members
Articles / Web Development / HTML
Tip/Trick

VisualBasic CLI Pipeline

Rate me:
Please Sign up or sign in to vote.
4.64/5 (4 votes)
21 Aug 2016CPOL 11.1K   48   3   2
How to use pipeline feature for linux programming

Introduction

CLI pipeline is the most important language programming feature of the data analysis industry/or server technique as it provides the basics of the analysis workflow automation and language hybrids feature. Almost all of the Linux server tools support this pipeline feature as nearly all of the management automation on the Linux server is based on the bash scripting.

Here is the pipeline API that provided in VisualBasic language server runtime: Microsoft.VisualBasic.CommandLine.CommandLine, and it makes this CLI pipeline more simple.

API Description
OpenStreamInput(String, String) As System.IO.StreamReader Open file input or std_in device as input
OpenStreamOutput(String) As System.IO.StreamWriter Open file output or std_out device as output

Usage Example

CLI

VB.NET
Imports System.IO
Imports Microsoft.VisualBasic.CommandLine
Imports Microsoft.VisualBasic.CommandLine.Reflection
Imports Microsoft.VisualBasic.Serialization.JSON

Module Program

    Public Function Main() As Integer
        Return GetType(Program).RunCLI(App.CommandLine)
    End Function

End Module

input bash

Bash
#!/bin/sh
echo "Hello World!!!"
echo "Test success!"

1. Only file supports

VB.NET
<ExportAPI("/file", Usage:="/file /in <file.txt> [/out <out.txt>]")>
Public Function OnlySupportsFile(args As CommandLine) As Integer
    Dim [in] As String = args("/in")
    Dim out As String = args.GetValue("/out", [in].TrimSuffix & ".output.json")
    Return [in].ReadAllLines.GetJson.SaveTo(out).CLICode
End Function

Test bash script:

Python
# file inputs
# read file input as text file and output the processed content as json
./bin/PipelineTest.exe /file /in ./input.sh /out ./1.txt

Test output:

["#!\/bin\/sh","echo \"Hello World!!!\"","echo \"Test success!\""]

2. Only std_in/std_out(Pipeline) supports

VB.NET
<ExportAPI("/std", Usage:="/std <input> <output>")>
Public Function JustStdDevice() As Integer
    Using input = Console.OpenStandardInput, output = New StreamWriter(Console.OpenStandardOutput)
        Call output.Write(New StreamReader(input).ReadToEnd.lTokens.GetJson)
    End Using

    Return 0
End Function

Test bash script:

Python
# std inputs
./input.sh | ./bin/PipelineTest.exe /std > ./2.txt

Test output:

VB.NET
["Hello World!!!","Test success!"]

3. Support both(file/Pipeline)

VB.NET
<ExportAPI("/pipe.Test", _
Usage:="/pipe.Test /in <file.txt/std_in> [/out <out.txt/std_out>]")>
Public Function SupportsBothFileAndPipeline(args As CommandLine) As Integer
    Using out = args.OpenStreamOutput("/out")
        Dim inData As String() = args.OpenStreamInput("/in").ReadToEnd.lTokens
        Call out.Write(inData.GetJson)
    End Using

    Return 0
End Function

Test bash script:

Python
# both supports
# just using standard input/output
./input.sh | ./bin/PipelineTest.exe /pipe.Test > ./3.txt
# using file io
./bin/PipelineTest.exe /pipe.Test /in ./input.sh /out ./4.txt

Test output: 3.txt:

["Hello World!!!","Test success!"]

Test output: 4.txt:

["#!\/bin\/sh","echo \"Hello World!!!\"","echo \"Test success!\""]

Image 1

Image 2

This CLI pipeline VisualBasic language programming feature was tested successfully on both Windows10 and Ubuntu.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead PANOMIX
China China
He is good and loves VisualBasic! Senior data scientist at PANOMIX


github: https://github.com/xieguigang

Comments and Discussions

 
QuestionCool Pin
LauraThomas23-Aug-16 3:41
LauraThomas23-Aug-16 3:41 
QuestionGreat Pin
Member 1269737722-Aug-16 4:41
Member 1269737722-Aug-16 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.