Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env pwsh
- Set-StrictMode -Version 5.0
- [string]$ScriptDirectory=Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
- try{
- . ("$ScriptDirectory/bottom.ps1")
- }catch{
- Write-Host "Error: Unable to include required script bottom.ps1"
- Exit 1
- }
- function display_message([int]$num,[string]$str){
- [string[]]$messagetext=@()
- [string]$usagetext=""
- [int]$returncode=1
- switch($num){
- 0 { $messagetext=@(
- "Bottom translator 0.0.1"
- )
- $returncode=0
- }
- 1 { $messagetext=@(
- "Fantastic (maybe) CLI for translating between bottom and human-readable text`n"
- "USAGE:"
- " bottomify [OPTIONS] <--bottomify|--regress> [text]...`n"
- "FLAGS:"
- " -b, --bottomify Translate text to bottom"
- " -h, --help Prints help information"
- " -r, --regress Translate bottom to human-readable text (futile)"
- " -V, --version Prints version information`n"
- "OPTIONS:"
- " -i, --input <INPUT> Input file [Default: stdin]"
- " -o, --output <OUTPUT> Output file [Default: stdout]`n"
- "ARGS:"
- " <text>..."
- )
- display_message 0
- $returncode=0
- }
- 2 { $messagetext=@(
- "error: The following required arguments were not provided:"
- " <--bottomify|--regress>`n"
- )
- $usagetext="[OPTIONS] <--bottomify|--regress> [text]..."
- }
- 3 { $messagetext=@(
- "error: The argument '--regress' cannot be used with one or more of the other specified arguments`n"
- )
- $usagetext="<--bottomify|--regress>"
- }
- 4 { $messagetext=@(
- "Error: Either input text or the --input options must be provided."
- ) }
- 5 { $messagetext=@(
- "error: Found argument '$str' which wasn't expected, or isn't valid in this context`n"
- )
- $usagetext="<--bottomify|--regress>"
- }
- 6 { $messagetext=@(
- "error: The argument '$str' was provided more than once, but cannot be used multiple times`n"
- )
- $usagetext="<--bottomify|--regress>"
- }
- 7 { $messagetext=@(
- "error: The argument '$str' was provided more than once, but cannot be used multiple times`n"
- )
- $usagetext="$str <--bottomify|--regress>"
- }
- 8 { $messagetext=@(
- "error: The argument '$str' requires a value but none was supplied`n"
- )
- $usagetext="$str <--bottomify|--regress>"
- }
- }
- Write-Host ($messagetext -join "`n")
- if($usagetext.length -gt 0){
- $messagetext=@(
- "USAGE:"
- " bottomify $usagetext`n"
- "For more information try --help"
- )
- Write-Host ($messagetext -join "`n")
- }
- return $returncode
- }
- function main([int]$argc,[string[]]$argv){
- [bool]$bottomify=$false
- [bool]$regress=$false
- [string]$str_input=""
- [string]$str_output=""
- [string]$str_text=""
- # skip the first argument as it is the script name
- [int]$i=1
- for($i=1;$i -lt $argv.count;$i++){
- if($argv[$i] -eq "-V" -or $argv[$i] -eq "--version"){
- return display_message 0
- }elseif($argv[$i] -eq "-h" -or $argv[$i] -eq "--help"){
- return display_message 1
- }elseif($bottomify -eq $true -and ($argv[$i] -eq "-b" -or $argv[$i] -eq "--bottomify")){
- return display_message 6 "--bottomify"
- }elseif($bottomify -eq $false -and ($argv[$i] -eq "-b" -or $argv[$i] -eq "--bottomify")){
- # note: specifying multiple of the same argument will cause an error
- $bottomify=$true
- }elseif($regress -eq $true -and ($argv[$i] -eq "-r" -or $argv[$i] -eq "--regress")){
- return display_message 6 "--regress"
- }elseif($regress -eq $false -and ($argv[$i] -eq "-r" -or $argv[$i] -eq "--regress")){
- $regress=$true
- }elseif($str_input.length -gt 0 -and ($argv[$i] -eq "-i" -or $argv[$i] -eq "--input" -or $argv[$i] -match "^--input=")){
- # error
- return display_message 7 "--input <INPUT>"
- }elseif($str_input.length -eq 0 -and ($argv[$i] -eq "-i" -or $argv[$i] -eq "--input" -or $argv[$i] -match "^--input=")){
- # assign input filename to $str_input
- if($argv[$i] -match "^--input=" -and $argv[$i].length -gt 8){
- $str_input=$argv[$i].substring(8).trim("`"", "'")
- }elseif(($i+1) -lt $argv.count -and -not ($argv[$i+1] -match "^-")){
- $str_input=$argv[$i+1]
- $i++
- }else{
- # no filename provided or the filename started with '-'
- # note: anything that starts with '-' seems to be assumed to be an argument
- return display_message 8 "--input <INPUT>"
- }
- }elseif($str_output.length -gt 0 -and ($argv[$i] -eq "-o" -or $argv[$i] -eq "--output" -or $argv[$i] -match "^--output=")){
- # error
- return display_message 7 "--output <OUTPUT>"
- }elseif($str_output.length -eq 0 -and ($argv[$i] -eq "-o" -or $argv[$i] -eq "--output" -or $argv[$i] -match "^--output=")){
- # assign output filename to $str_output
- if($argv[$i] -match "^--output=" -and $argv[$i].length -gt 9){
- $str_output=$argv[$i].substring(9).trim("`"", "'")
- }elseif(($i+1) -lt $argv.count -and -not ($argv[$i+1] -match "^-")){
- $str_output=$argv[$i+1]
- $i++
- }else{
- # no filename provided or the filename started with '-'
- return display_message 8 "--output <OUTPUT>"
- }
- }elseif($str_text.length -eq 0 -and -not ($argv[$i] -match "^-")){
- # process text arguments and assign to $str_text
- # note: only the first text argument is processed
- $str_text=$argv[$i]
- }elseif($argv[$i] -match "^-"){
- # try to match the behavior of unexpected arguments
- return display_message 5 $argv[$i]
- }
- }
- if($bottomify -eq $false -and $regress -eq $false){
- # one of these options is required
- return display_message 2
- }elseif($bottomify -eq $true -and $regress -eq $true){
- # can't use both options at the same time
- return display_message 3
- }
- [bool]$text_input=$($str_text.length -gt 0)
- [bool]$file_input=$($str_input.length -gt 0)
- [bool]$file_output=$($str_output.length -gt 0)
- if(($text_input -eq $true -and $file_input -eq $true) -or ($text_input -eq $false -and $file_input -eq $false)){
- # need input text or a file but not both
- return display_message 4
- }
- [System.Text.Encoding]$enc=[System.Text.Encoding]::UTF8
- if($text_input -ne $true){
- # read in file $str_input and assign to $str_text
- # let powershell throw any obvious file i/o errors
- $str_text=[System.IO.File]::ReadAllText($str_input, $enc)
- }
- [string]$str_result=if($bottomify -eq $true){
- $bottom.encode_string($str_text)
- }else{
- $bottom.decode_string($str_text)
- }
- if($file_output -eq $true){
- # write $str_result to file $str_output
- # let powershell throw any obvious file i/o errors
- [System.IO.File]::WriteAllText($str_output, $str_result, $enc)
- }else{
- Write-Host $str_result
- }
- return 0
- }
- # start process
- Exit main ($args.count+1) (@($MyInvocation.MyCommand.Name)+$args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement