Advertisement
krishean

bottomify.ps1

Mar 30th, 2021
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env pwsh
  2. Set-StrictMode -Version 5.0
  3. [string]$ScriptDirectory=Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
  4. try{
  5.     . ("$ScriptDirectory/bottom.ps1")
  6. }catch{
  7.     Write-Host "Error: Unable to include required script bottom.ps1"
  8.     Exit 1
  9. }
  10. function display_message([int]$num,[string]$str){
  11.     [string[]]$messagetext=@()
  12.     [string]$usagetext=""
  13.     [int]$returncode=1
  14.     switch($num){
  15.         0 { $messagetext=@(
  16.                 "Bottom translator 0.0.1"
  17.             )
  18.             $returncode=0
  19.         }
  20.         1 { $messagetext=@(
  21.                 "Fantastic (maybe) CLI for translating between bottom and human-readable text`n"
  22.                 "USAGE:"
  23.                 "    bottomify [OPTIONS] <--bottomify|--regress> [text]...`n"
  24.                 "FLAGS:"
  25.                 "    -b, --bottomify    Translate text to bottom"
  26.                 "    -h, --help         Prints help information"
  27.                 "    -r, --regress      Translate bottom to human-readable text (futile)"
  28.                 "    -V, --version      Prints version information`n"
  29.                 "OPTIONS:"
  30.                 "    -i, --input <INPUT>      Input file [Default: stdin]"
  31.                 "    -o, --output <OUTPUT>    Output file [Default: stdout]`n"
  32.                 "ARGS:"
  33.                 "    <text>..."
  34.             )
  35.             display_message 0
  36.             $returncode=0
  37.         }
  38.         2 { $messagetext=@(
  39.                 "error: The following required arguments were not provided:"
  40.                 "    <--bottomify|--regress>`n"
  41.             )
  42.             $usagetext="[OPTIONS] <--bottomify|--regress> [text]..."
  43.         }
  44.         3 { $messagetext=@(
  45.                 "error: The argument '--regress' cannot be used with one or more of the other specified arguments`n"
  46.             )
  47.             $usagetext="<--bottomify|--regress>"
  48.         }
  49.         4 { $messagetext=@(
  50.             "Error: Either input text or the --input options must be provided."
  51.         ) }
  52.         5 { $messagetext=@(
  53.                 "error: Found argument '$str' which wasn't expected, or isn't valid in this context`n"
  54.             )
  55.             $usagetext="<--bottomify|--regress>"
  56.         }
  57.         6 { $messagetext=@(
  58.                 "error: The argument '$str' was provided more than once, but cannot be used multiple times`n"
  59.             )
  60.             $usagetext="<--bottomify|--regress>"
  61.         }
  62.         7 { $messagetext=@(
  63.                 "error: The argument '$str' was provided more than once, but cannot be used multiple times`n"
  64.             )
  65.             $usagetext="$str <--bottomify|--regress>"
  66.         }
  67.         8 { $messagetext=@(
  68.                 "error: The argument '$str' requires a value but none was supplied`n"
  69.             )
  70.             $usagetext="$str <--bottomify|--regress>"
  71.         }
  72.     }
  73.     Write-Host ($messagetext -join "`n")
  74.     if($usagetext.length -gt 0){
  75.         $messagetext=@(
  76.             "USAGE:"
  77.             "    bottomify $usagetext`n"
  78.             "For more information try --help"
  79.         )
  80.         Write-Host ($messagetext -join "`n")
  81.     }
  82.     return $returncode
  83. }
  84. function main([int]$argc,[string[]]$argv){
  85.     [bool]$bottomify=$false
  86.     [bool]$regress=$false
  87.     [string]$str_input=""
  88.     [string]$str_output=""
  89.     [string]$str_text=""
  90.     # skip the first argument as it is the script name
  91.     [int]$i=1
  92.     for($i=1;$i -lt $argv.count;$i++){
  93.         if($argv[$i] -eq "-V" -or $argv[$i] -eq "--version"){
  94.             return display_message 0
  95.         }elseif($argv[$i] -eq "-h" -or $argv[$i] -eq "--help"){
  96.             return display_message 1
  97.         }elseif($bottomify -eq $true -and ($argv[$i] -eq "-b" -or $argv[$i] -eq "--bottomify")){
  98.             return display_message 6 "--bottomify"
  99.         }elseif($bottomify -eq $false -and ($argv[$i] -eq "-b" -or $argv[$i] -eq "--bottomify")){
  100.             # note: specifying multiple of the same argument will cause an error
  101.             $bottomify=$true
  102.         }elseif($regress -eq $true -and ($argv[$i] -eq "-r" -or $argv[$i] -eq "--regress")){
  103.             return display_message 6 "--regress"
  104.         }elseif($regress -eq $false -and ($argv[$i] -eq "-r" -or $argv[$i] -eq "--regress")){
  105.             $regress=$true
  106.         }elseif($str_input.length -gt 0 -and ($argv[$i] -eq "-i" -or $argv[$i] -eq "--input" -or $argv[$i] -match "^--input=")){
  107.             # error
  108.             return display_message 7 "--input <INPUT>"
  109.         }elseif($str_input.length -eq 0 -and ($argv[$i] -eq "-i" -or $argv[$i] -eq "--input" -or $argv[$i] -match "^--input=")){
  110.             # assign input filename to $str_input
  111.             if($argv[$i] -match "^--input=" -and $argv[$i].length -gt 8){
  112.                 $str_input=$argv[$i].substring(8).trim("`"", "'")
  113.             }elseif(($i+1) -lt $argv.count -and -not ($argv[$i+1] -match "^-")){
  114.                 $str_input=$argv[$i+1]
  115.                 $i++
  116.             }else{
  117.                 # no filename provided or the filename started with '-'
  118.                 # note: anything that starts with '-' seems to be assumed to be an argument
  119.                 return display_message 8 "--input <INPUT>"
  120.             }
  121.         }elseif($str_output.length -gt 0 -and ($argv[$i] -eq "-o" -or $argv[$i] -eq "--output" -or $argv[$i] -match "^--output=")){
  122.             # error
  123.             return display_message 7 "--output <OUTPUT>"
  124.         }elseif($str_output.length -eq 0 -and ($argv[$i] -eq "-o" -or $argv[$i] -eq "--output" -or $argv[$i] -match "^--output=")){
  125.             # assign output filename to $str_output
  126.             if($argv[$i] -match "^--output=" -and $argv[$i].length -gt 9){
  127.                 $str_output=$argv[$i].substring(9).trim("`"", "'")
  128.             }elseif(($i+1) -lt $argv.count -and -not ($argv[$i+1] -match "^-")){
  129.                 $str_output=$argv[$i+1]
  130.                 $i++
  131.             }else{
  132.                 # no filename provided or the filename started with '-'
  133.                 return display_message 8 "--output <OUTPUT>"
  134.             }
  135.         }elseif($str_text.length -eq 0 -and -not ($argv[$i] -match "^-")){
  136.             # process text arguments and assign to $str_text
  137.             # note: only the first text argument is processed
  138.             $str_text=$argv[$i]
  139.         }elseif($argv[$i] -match "^-"){
  140.             # try to match the behavior of unexpected arguments
  141.             return display_message 5 $argv[$i]
  142.         }
  143.     }
  144.     if($bottomify -eq $false -and $regress -eq $false){
  145.         # one of these options is required
  146.         return display_message 2
  147.     }elseif($bottomify -eq $true -and $regress -eq $true){
  148.         # can't use both options at the same time
  149.         return display_message 3
  150.     }
  151.     [bool]$text_input=$($str_text.length -gt 0)
  152.     [bool]$file_input=$($str_input.length -gt 0)
  153.     [bool]$file_output=$($str_output.length -gt 0)
  154.     if(($text_input -eq $true -and $file_input -eq $true) -or ($text_input -eq $false -and $file_input -eq $false)){
  155.         # need input text or a file but not both
  156.         return display_message 4
  157.     }
  158.     [System.Text.Encoding]$enc=[System.Text.Encoding]::UTF8
  159.     if($text_input -ne $true){
  160.         # read in file $str_input and assign to $str_text
  161.         # let powershell throw any obvious file i/o errors
  162.         $str_text=[System.IO.File]::ReadAllText($str_input, $enc)
  163.     }
  164.     [string]$str_result=if($bottomify -eq $true){
  165.         $bottom.encode_string($str_text)
  166.     }else{
  167.         $bottom.decode_string($str_text)
  168.     }
  169.     if($file_output -eq $true){
  170.         # write $str_result to file $str_output
  171.         # let powershell throw any obvious file i/o errors
  172.         [System.IO.File]::WriteAllText($str_output, $str_result, $enc)
  173.     }else{
  174.         Write-Host $str_result
  175.     }
  176.     return 0
  177. }
  178. # start process
  179. Exit main ($args.count+1) (@($MyInvocation.MyCommand.Name)+$args)
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement