Skip to content

Commit

Permalink
Merge pull request #170 from nfdi4plants/filenameAttribute
Browse files Browse the repository at this point in the history
Throw warning when character length of file name arguments exceed 31
  • Loading branch information
HLWeil authored Dec 21, 2022
2 parents c71fd91 + fd75cc8 commit 5167eec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
30 changes: 24 additions & 6 deletions src/ArcCommander/ArgumentProcessing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,23 @@ module ArgumentProcessing =
Tooltip : string
IsMandatory : bool
IsFlag : bool
IsFileName : bool
}

let createAnnotatedArgument arg tt mand isFlag =
let createAnnotatedArgument arg tt mand isFlag isFN =
{
Arg = arg
Tooltip = tt
IsMandatory = mand
IsFlag = isFlag
IsFileName = isFN
}

// classic forbidden symbols for file names in Windows (which also includes Linux/macOS)
let private forbiddenSymbols = [|'/'; '\\'; '"'; '>'; '<'; '?'; '='; '*'; '|'|]

/// Characters that must not occur in file names.
let private forbiddenChars =
// classic forbidden symbols for file names in Windows (which also includes Linux/macOS)
let forbiddenSymbols = [|'/'; '\\'; '"'; '>'; '<'; '?'; '='; '*'; '|'|]
let controlChars = Array.init 32 (fun i -> char i)
Array.append controlChars forbiddenSymbols

Expand Down Expand Up @@ -77,6 +80,12 @@ module ArgumentProcessing =
log.Error $"Identifier/filename \"{str}\" is a reserved filename. Please choose another one."
raise (Exception "")

/// Takes a string and checks if it is longer than 31 chars
let private checkForNameLength (str : string) =
let log = Logging.createLogger "ArgumentProcessingCheckForFileNameLength"
if seq str |> Seq.length |> (<) 31 then
log.Warn $"Identifier/filename \"{str}\" is longer than 31 characters, which might cause problems in excel sheets."

/// Returns true if the argument flag of name k was given by the user.
let containsFlag k (arguments : Map<string,Argument>) =
let log = Logging.createLogger "ArgumentProcessingContainsFlagLog"
Expand Down Expand Up @@ -144,7 +153,7 @@ module ArgumentProcessing =
| [||] ->
let toolTip = (FSharpValue.MakeUnion (unionCase, [||]) :?> 'T).Usage
let value,isFlag = if Map.containsKey unionCase.Name m then Some Flag,true else None,true
unionCase.Name,createAnnotatedArgument value toolTip isMandatory isFlag
unionCase.Name,createAnnotatedArgument value toolTip isMandatory isFlag isFileAttribute
| [|c|] when c.PropertyType.Name = "String" ->
let toolTip = (FSharpValue.MakeUnion (unionCase, [|box ""|]) :?> 'T).Usage
let value, isFlag =
Expand All @@ -155,13 +164,14 @@ module ArgumentProcessing =
if isFileAttribute then
iterForbiddenChars str
checkForReservedFns str
checkForNameLength str
replaceSpace str
else str
Field adjustedStr
|> Some,
false
| None -> None, false
unionCase.Name, createAnnotatedArgument value toolTip isMandatory isFlag
unionCase.Name, createAnnotatedArgument value toolTip isMandatory isFlag isFileAttribute
| _ ->
log.Fatal($"Cannot parse argument {unionCase.Name} because its parsing rules were not yet implemented.")
raise (Exception(""))
Expand Down Expand Up @@ -239,13 +249,20 @@ module ArgumentProcessing =
elif arg.IsFlag then
sprintf "Remove # below to set flag: %s" arg.Tooltip
else sprintf "%s" arg.Tooltip
let fileComment =
$"""# FileName: The value of this argument will be used as a file or folder name.
# FileName: Please refrain from using the following characters: {forbiddenSymbols |> Seq.map string |> String.concat " "}.
# FileName: Please write a value of length at most 31 characters."""
let value =
match arg.Arg with
| Some (Flag) -> sprintf "%s" key
| Some (Field v) -> sprintf "%s:%s" key v
| None when arg.IsFlag -> sprintf "#%s" key
| None -> sprintf "%s:" key
sprintf "#%s\n%s" comment value
if arg.IsFileName then
sprintf "#%s\n%s\n%s" comment fileComment value
else
sprintf "#%s\n%s" comment value
)
|> Array.reduce (fun a b -> a + "\n\n" + b)
|> sprintf "%s\n\n%s" header
Expand All @@ -260,6 +277,7 @@ module ArgumentProcessing =
if key.Contains "Identifier" then
iterForbiddenChars trValu
checkForReservedFns trValu
checkForNameLength trValu
replaceSpace trValu
else trValu
match s.Split c with
Expand Down
4 changes: 2 additions & 2 deletions src/ArcCommander/CLIArguments/AssayArgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ type AssayUpdateArgs =

/// CLI arguments for interactively editing existing assay metadata.
type AssayEditArgs =
| [<AltCommandLine("-s")>][<Unique>][<FileNameAttribute>] StudyIdentifier of study_identifier : string
| [<Mandatory>][<AltCommandLine("-a")>][<Unique>][<FileNameAttribute>] AssayIdentifier of assay_identifier : string
| [<AltCommandLine("-s")>][<Unique>][<FileName>] StudyIdentifier of study_identifier : string
| [<Mandatory>][<AltCommandLine("-a")>][<Unique>][<FileName>] AssayIdentifier of assay_identifier : string

interface IArgParserTemplate with
member this.Usage =
Expand Down
5 changes: 3 additions & 2 deletions src/ArcCommander/CLIArguments/InvestigationArgs.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
namespace ArcCommander.CLIArguments

open Argu
open ArcCommander.ArgumentProcessing

/// CLI arguments for creating a new investigation file for the arc
// in the case of investigations 'empty' does not mean empty file but rather an
// investigation without studies/assays. To reflect the need for metadata here,
// this command is called `create` instead of `init`
type InvestigationCreateArgs =
| [<Mandatory>][<AltCommandLine("-i")>][<Unique>] Identifier of investigation_identifier:string
| [<Mandatory>][<AltCommandLine("-i")>][<Unique>][<FileName>] Identifier of investigation_identifier:string
| [<Unique>] Title of title:string
| [<Unique>] Description of description:string
| [<Unique>] SubmissionDate of submission_date:string
Expand All @@ -24,7 +25,7 @@ type InvestigationCreateArgs =

/// CLI arguments updating the arc's existing investigation file
type InvestigationUpdateArgs =
| [<Mandatory>][<AltCommandLine("-i")>][<Unique>] Identifier of investigation_identifier:string
| [<Mandatory>][<AltCommandLine("-i")>][<Unique>][<FileName>] Identifier of investigation_identifier:string
| [<Unique>] Title of title:string
| [<Unique>] Description of description:string
| [<Unique>] SubmissionDate of submission_date:string
Expand Down

0 comments on commit 5167eec

Please sign in to comment.