Skip to content

Commit a61b508

Browse files
dsymeYoussef1313cartermpgewarren
authored
F# plaintext printing documentation (#19632)
* plaintext printing documentation * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update plaintext-formatting.md * Update plaintext-formatting.md * add printf information * add printf information * add printf information * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Youssef Victor <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update index.md * Update toc.yml * Update index.md * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Genevieve Warren <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/plaintext-formatting.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> * Update docs/fsharp/language-reference/fsharp-interactive-options.md Co-authored-by: Phillip Carter <[email protected]> Co-authored-by: Youssef Victor <[email protected]> Co-authored-by: Phillip Carter <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent 4846604 commit a61b508

File tree

4 files changed

+552
-14
lines changed

4 files changed

+552
-14
lines changed

docs/fsharp/language-reference/fsharp-interactive-options.md

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
---
2-
title: Interactive Options
2+
title: Interactive options
33
description: Learn about the command-line options supported by F# Interactive, fsi.exe.
4-
ms.date: 05/16/2016
4+
ms.date: 07/22/2020
55
---
6-
# F# Interactive Options
6+
# F# Interactive options
77

8-
> [!NOTE]
9-
> This article currently describes the experience for Windows only. It will be rewritten.
8+
This article describes the command-line options supported by F# Interactive, `fsi.exe`. F# Interactive accepts many of the same command line options as the F# compiler, but also accepts some additional options.
109

11-
This topic describes the command-line options supported by F# Interactive, `fsi.exe`. F# Interactive accepts many of the same command line options as the F# compiler, but also accepts some additional options.
10+
## Use F# Interactive for scripting
1211

13-
## Using F# Interactive for Scripting
12+
F# Interactive, `dotnet fsi`, can be launched interactively, or it can be launched from the command line to run a script. The command line syntax is
1413

15-
F# Interactive, `fsi.exe`, can be launched interactively, or it can be launched from the command line to run a script. The command line syntax is
16-
17-
```console
18-
> fsi.exe [options] [ script-file [arguments] ]
14+
```dotnetcli
15+
dotnet fsi [options] [ script-file [arguments] ]
1916
```
2017

2118
The file extension for F# script files is `.fsx`.
@@ -52,8 +49,6 @@ Where lists appear in F# Interactive option arguments, list elements are separat
5249
|**--quotations-debug**|Specifies that extra debugging information should be emitted for expressions that are derived from F# quotation literals and reflected definitions. The debug information is added to the custom attributes of an F# expression tree node. See [Code Quotations](code-quotations.md) and [Expr.CustomAttributes](https://msdn.microsoft.com/library/eb89943f-5f5b-474e-b125-030ca412edb3).|
5350
|**--readline**[**+**&#124;**-**]|Enable or disable tab completion in interactive mode.|
5451
|**--reference:&lt;filename&gt;**<br /><br />**-r:&lt;filename&gt;**|Same as the **fsc.exe** compiler option. For more information, see [Compiler Options](compiler-options.md).|
55-
|**--shadowcopyreferences**[**+**&#124;**-**]|Prevents references from being locked by the F# Interactive process.|
56-
|**--simpleresolution**|Resolves assembly references using directory-based rules rather than MSBuild resolution.|
5752
|**--tailcalls**[**+**&#124;**-**]|Enable or disable the use of the tail IL instruction, which causes the stack frame to be reused for tail recursive functions. This option is enabled by default.|
5853
|**--targetprofile:&lt;string&gt;**|Specifies target framework profile of this assembly. Valid values are mscorlib, netcore or netstandard. The default is mscorlib.|
5954
|**--use:&lt;filename&gt;**|Tells the interpreter to use the given file on startup as initial input.|
@@ -62,7 +57,107 @@ Where lists appear in F# Interactive option arguments, list elements are separat
6257
|**--warnaserror**[**+**&#124;**-**]|Same as the **fsc.exe** compiler option. For more information, see [Compiler Options](compiler-options.md).|
6358
|**--warnaserror**[**+**&#124;**-**]:**&lt;int-list&gt;**|Same as the **fsc.exe** compiler option. For more information, see [Compiler Options](compiler-options.md).|
6459

65-
## Related Topics
60+
## F# Interactive structured printing
61+
62+
F# Interactive (`dotnet fsi`) uses an extended version of [structured plain text formatting](plaintext-formatting.md) to
63+
report values.
64+
65+
1. All features of `%A` plain text formatting are supported, and some are additionally customizable.
66+
67+
2. Printing is colorized if colors are supported by the output console.
68+
69+
3. A limit is placed on the length of strings shown, unless you explicitly evaluate that string.
70+
71+
4. A set of user-definable settings are available via the `fsi` object.
72+
73+
The available settings to customize plain text printing for reported values are:
74+
75+
```fsharp
76+
open System.Globalization
77+
78+
fsi.FormatProvider <- CultureInfo("de-DE") // control the default culture for primitives
79+
80+
fsi.PrintWidth <- 120 // Control the width used for structured printing
81+
82+
fsi.PrintDepth <- 10 // Control the maximum depth of nested printing
83+
84+
fsi.PrintLength <- 10 // Control the length of lists and arrays
85+
86+
fsi.PrintSize <- 100 // Control the maximum overall object count
87+
88+
fsi.ShowProperties <- false // Control whether properties of .NET objects are shown by default
89+
90+
fsi.ShowIEnumerable <- false // Control whether sequence values are expanded by default
91+
92+
fsi.ShowDeclarationValues <- false // Control whether values are shown for declaration outputs
93+
```
94+
95+
### Customize with `AddPrinter` and `AddPrintTransformer`
96+
97+
Printing in F# Interactive outputs can be customized by using `fsi.AddPrinter` and `fsi.AddPrintTransformer`.
98+
The first function gives text to replace the printing of an object. The second function returns a surrogate object to display
99+
instead. For example, consider the following F# code:
100+
101+
```fsharp
102+
open System
103+
104+
fsi.AddPrinter<DateTime>(fun dt -> dt.ToString("s"))
105+
106+
type DateAndLabel =
107+
{ Date: DateTime
108+
Label: string }
109+
110+
let newYearsDay1999 =
111+
{ Date = DateTime(1999, 1, 1)
112+
Label = "New Year" }
113+
```
114+
115+
If you execute the example in F# Interactive, it outputs based on the formatting option set. In this case, it affects the formatting of date and time:
116+
117+
```console
118+
type DateAndLabel =
119+
{ Date: DateTime
120+
Label: string }
121+
val newYearsDay1999 : DateAndLabel = { Date = 1999-01-01T00:00:00
122+
Label = "New Year" }
123+
```
124+
125+
`fsi.AddPrintTransformer` can be used to give a surrogate object for printing:
126+
127+
```fsharp
128+
type MyList(values: int list) =
129+
member _.Values = values
130+
131+
fsi.AddPrintTransformer(fun (x:MyList) -> box x.Values)
132+
133+
let x = MyList([1..10])
134+
```
135+
136+
This outputs:
137+
138+
```console
139+
val x : MyList = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
140+
```
141+
142+
If the transformer function passed to `fsi.AddPrintTransformer` returns `null`, then the print transformer is ignored.
143+
This can be used to filter any input value by starting with type `obj`. For example:
144+
145+
```fsharp
146+
fsi.AddPrintTransformer(fun (x:obj) ->
147+
match x with
148+
| :? string as s when s = "beep" -> box ["quack"; "quack"; "quack"]
149+
| _ -> null)
150+
151+
let y = "beep"
152+
```
153+
154+
This outputs:
155+
156+
```console
157+
val y : string = ["quack"; "quack"; "quack"]
158+
```
159+
160+
## Related topics
66161

67162
|Title|Description|
68163
|-----|-----------|

docs/fsharp/language-reference/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The following table shows reference topics available that describe language conc
4545
|[Signatures](signature-files.md)|Describes signatures and signature files. A signature file contains information about the public signatures of a set of F# program elements, such as types, namespaces, and modules. It can be used to specify the accessibility of these program elements.|
4646
|[XML Documentation](xml-documentation.md)|Describes support for generating documentation files for XML doc comments, also known as triple slash comments. You can produce documentation from code comments in F# just as in other .NET languages.|
4747
|[Verbose Syntax](verbose-syntax.md)|Describes the syntax for F# constructs when lightweight syntax is not enabled. Verbose syntax is indicated by the `#light "off"` directive at the top of the code file.|
48+
|[Plain Text Formatting](plaintext-formatting.md)|Learn how to use sprintf and other plain text formatting in F# applications and scripts.|
4849

4950
## F# Types
5051

0 commit comments

Comments
 (0)