spacedriveapp/spacedrive · error · anyhow::Error

Invalid sort option: {}. Valid options are: name, modified,

Error message

Invalid sort option: {}. Valid options are: name, modified, size, type

What it means

`sd-cli file list --sort-by <value>` lowercases the value and matches it against the four supported `DirectorySortBy` variants (name, modified, size, type). Any other string bails with this message before the directory query is dispatched to the core, so nothing is listed and the daemon is never contacted.

Source

Thrown at apps/cli/src/domains/file/mod.rs:60

			print_output!(ctx, &file_info, |info: &Option<sd_core::domain::File>| {
				match info {
					Some(file) => {
						println!("{}", serde_json::to_string_pretty(file).unwrap());
					}
					None => {
						println!("File not found or not indexed in Spacedrive");
					}
				}
			});
		}
		FileCmd::List(args) => {
			let sort_by = match args.sort_by.to_lowercase().as_str() {
				"name" => sd_core::ops::files::query::DirectorySortBy::Name,
				"modified" => sd_core::ops::files::query::DirectorySortBy::Modified,
				"size" => sd_core::ops::files::query::DirectorySortBy::Size,
				"type" => sd_core::ops::files::query::DirectorySortBy::Type,
				_ => {
					anyhow::bail!(
						"Invalid sort option: {}. Valid options are: name, modified, size, type",
						args.sort_by
					);
				}
			};
			let directory_listing =
				list_directory(ctx, &args.path, args.limit, args.include_hidden, sort_by).await?;
			print_output!(
				ctx,
				&directory_listing,
				|listing: &sd_core::ops::files::query::DirectoryListingOutput| {
					println!("Directory: {}", args.path.display());
					println!("Found {} items:", listing.files.len());
					println!();

					// Create a table to display the results
					let mut table = comfy_table::Table::new();
					table.load_preset(UTF8_BORDERS_ONLY);

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Use one of the four accepted values exactly: name, modified, size, type (case-insensitive).
  2. Check `sd-cli file list --help` for the accepted options of your build.
  3. As a maintainer, replace the free-text String arg with a clap ValueEnum so invalid values are rejected at parse time with a proper usage message.

Example fix

// before (apps/cli/src/domains/file/args.rs + mod.rs)
pub sort_by: String,
let sort_by = match args.sort_by.to_lowercase().as_str() { /* ... */ _ => anyhow::bail!("Invalid sort option: {}...") };

// after: clap validates at parse time
#[derive(clap::ValueEnum, Clone, Copy)]
pub enum SortByArg { Name, Modified, Size, Type }
#[arg(long, value_enum, default_value_t = SortByArg::Name)]
pub sort_by: SortByArg,
// then map without a failing arm
let sort_by = match args.sort_by { SortByArg::Name => DirectorySortBy::Name, SortByArg::Modified => DirectorySortBy::Modified, SortByArg::Size => DirectorySortBy::Size, SortByArg::Type => DirectorySortBy::Type };
Defensive patterns

Strategy: validation

Validate before calling

fn parse_sort(v: &str) -> Option<DirectorySortBy> {
    match v.trim().to_lowercase().as_str() {
        "name" => Some(DirectorySortBy::Name),
        "modified" => Some(DirectorySortBy::Modified),
        "size" => Some(DirectorySortBy::Size),
        "type" => Some(DirectorySortBy::Type),
        _ => None,
    }
}
// before dispatching:
let sort_by = parse_sort(&raw).ok_or_else(|| anyhow::anyhow!("sort must be name|modified|size|type"))?;

Type guard

pub fn is_valid_sort(v: &str) -> bool {
    matches!(v.trim().to_lowercase().as_str(), "name" | "modified" | "size" | "type")
}

Try / catch

match run_list(args).await {
    Err(e) if e.to_string().contains("Invalid sort option") => { print_usage_sort(); Err(e) }
    other => other,
}

Prevention

When it happens

Trigger: Passing natural but unsupported keys such as `--sort-by date`, `--sort-by created`, `--sort-by name-desc`, or a typo like `--sort-by siez`; note the check is case-insensitive so `Name` works but `name ` (trailing whitespace) does not.

Common situations: Users assuming the CLI accepts the same sort keys as the desktop UI; scripts ported from other tools (e.g. `ls -t` habits) that map to 'time' rather than 'modified'.

Related errors


AI-assisted analysis of spacedriveapp/spacedrive@6dfeccf211 (2026-08-16). Data as JSON: /api/errors/e2dc832f5d18b8bf. Report an issue: GitHub.