7 people like it.

Read a password from the command line, masking input

Read a password from the command line, masking input with an asterisk. Only tested on Windows 10.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
let readPassword () =
    let rec readMask pw =
        let k = System.Console.ReadKey()
        match k.Key with
        | System.ConsoleKey.Enter -> pw
        | System.ConsoleKey.Escape -> pw
        | System.ConsoleKey.Backspace ->
            match pw with
            | [] -> readMask []
            | _::t ->
                System.Console.Write " \b"
                readMask t
        | _ ->
            System.Console.Write "\b*"
            readMask (k.KeyChar::pw)
    let password = readMask [] |> Seq.rev |> System.String.Concat
    System.Console.WriteLine ()
    password
val readPassword : unit -> string

Full name: Script.readPassword
val readMask : (char list -> char list)
val pw : char list
val k : System.ConsoleKeyInfo
namespace System
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.ReadKey() : System.ConsoleKeyInfo
System.Console.ReadKey(intercept: bool) : System.ConsoleKeyInfo
property System.ConsoleKeyInfo.Key: System.ConsoleKey
type ConsoleKey =
  | Backspace = 8
  | Tab = 9
  | Clear = 12
  | Enter = 13
  | Pause = 19
  | Escape = 27
  | Spacebar = 32
  | PageUp = 33
  | PageDown = 34
  | End = 35
  ...

Full name: System.ConsoleKey
field System.ConsoleKey.Enter = 13
field System.ConsoleKey.Escape = 27
field System.ConsoleKey.Backspace = 8
val t : char list
System.Console.Write(value: string) : unit
   (+0 other overloads)
System.Console.Write(value: obj) : unit
   (+0 other overloads)
System.Console.Write(value: uint64) : unit
   (+0 other overloads)
System.Console.Write(value: int64) : unit
   (+0 other overloads)
System.Console.Write(value: uint32) : unit
   (+0 other overloads)
System.Console.Write(value: int) : unit
   (+0 other overloads)
System.Console.Write(value: float32) : unit
   (+0 other overloads)
System.Console.Write(value: decimal) : unit
   (+0 other overloads)
System.Console.Write(value: float) : unit
   (+0 other overloads)
System.Console.Write(buffer: char []) : unit
   (+0 other overloads)
property System.ConsoleKeyInfo.KeyChar: char
val password : string
module Seq

from Microsoft.FSharp.Collections
val rev : source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.rev
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
System.String(value: nativeptr<char>) : unit
System.String(value: nativeptr<sbyte>) : unit
System.String(value: char []) : unit
System.String(c: char, count: int) : unit
System.String(value: nativeptr<char>, startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
System.String(value: char [], startIndex: int, length: int) : unit
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : unit
System.String.Concat([<System.ParamArray>] values: string []) : string
   (+0 other overloads)
System.String.Concat(values: System.Collections.Generic.IEnumerable<string>) : string
   (+0 other overloads)
System.String.Concat<'T>(values: System.Collections.Generic.IEnumerable<'T>) : string
   (+0 other overloads)
System.String.Concat([<System.ParamArray>] args: obj []) : string
   (+0 other overloads)
System.String.Concat(arg0: obj) : string
   (+0 other overloads)
System.String.Concat(str0: string, str1: string) : string
   (+0 other overloads)
System.String.Concat(arg0: obj, arg1: obj) : string
   (+0 other overloads)
System.String.Concat(str0: string, str1: string, str2: string) : string
   (+0 other overloads)
System.String.Concat(arg0: obj, arg1: obj, arg2: obj) : string
   (+0 other overloads)
System.String.Concat(str0: string, str1: string, str2: string, str3: string) : string
   (+0 other overloads)
System.Console.WriteLine() : unit
   (+0 other overloads)
System.Console.WriteLine(value: string) : unit
   (+0 other overloads)
System.Console.WriteLine(value: obj) : unit
   (+0 other overloads)
System.Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
System.Console.WriteLine(value: int64) : unit
   (+0 other overloads)
System.Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
System.Console.WriteLine(value: int) : unit
   (+0 other overloads)
System.Console.WriteLine(value: float32) : unit
   (+0 other overloads)
System.Console.WriteLine(value: float) : unit
   (+0 other overloads)
System.Console.WriteLine(value: decimal) : unit
   (+0 other overloads)

More information

Link:http://fssnip.net/2Vq
Posted:8 years ago
Author:TMVector
Tags: console