Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

VisualBasic CLI Pipeline

4.64/5 (4 votes)
21 Aug 2016CPOL 11.4K   48  
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)